Xcode change image on button that is not sender - xcode

I am trying to change a image of a button that is not the senders, I have tried to link the code to the buttons and it seems not to work
I tried to link the 3 buttons to the -(IBAction)Button1:(id)sender; it still does not work.
.h
-(IBAction)Button1:(id)sender;
-(IBAction)ButtonUn1:(id)sender;
-(IBAction)ButtonDef1:(id)sender;
.m
-(IBAction)Button1:(id)sender
{
UIImage *btnImage1 = [UIImage imageNamed:#"button_Not_At_all_R.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];
UIImage *btnImage2 = [UIImage imageNamed:#"Button_Unsure.png"];
UIImage *btnImage3 = [UIImage imageNamed:#"Button_Definitely.png"];
[ButtonUn1 setImage:btnImage2 forState:UIControlStateNormal];
[ButoonDef1 setImage:btnImage3 forState:UIControlStateNormal];
}
Thanks for any help.

You need to create an IBOutlet for your button and link it to access it directly:
IBOutlet UIButton* buttonToChangePicture;
The IBAction ist only used to access an event.
Edit:
Another option is to use viewWithTag.
UIButton* button = (UIButton*)[myView viewWithTag:555]
You need to set a unique tag for your button first (555 in my example)
There is no other way to access another button in an IBAction

Related

How to change UIImageview image by clicking on that image instead of button click in Iphone?

How to change image by click on that image in Iphone.Instead of button click I want to click on image.Which method can I use UIImageView.
Use a button and make your picture the picture for the button. You can do all this in Storyboard and then right click the button and drag to the view you want to go to. It should work just like that.
After you drag the button to the storyboard go to the property inspector. Make the button a custom button in the drop down list and then use the image field to select your image. You will need to size the button correctly and set the fill property to "size to fit." the other button behaviors are all customizable if you don't want normal button behavior, i.e. visually reacting to a touch.
This can be done by using touchevents also
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([[touches anyObject] view] == self.m_ImageViewObjectName) {
//Here you can change the image of UIImageView
}
}
You have to set tapRecognizer for your ImageView. It'll detect tap on your ImageView and call the method -clickEventOnImage.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(clickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
//Don't forget to set the userInteractionEnabled to YES, by default It's NO.
myImageView.userInteractionEnabled = YES;
[myImageView addGestureRecognizer:tapRecognizer];
Add your clickEventOnImage: will look like this :
-(void) clickEventOnImage:(id) sender
{
[myImageView setImage:[UIImage imageNamed: #"myNewImageName.png"]];
}

How to I launch a view controller from a tap gesture

Apologies if this is a basic question but I am new to Xcode and have a storyboard app and in the storyboard (with no segue) I have a view controller with an embedded map view.
On the storyboard screen I have an image with a tap gesture linked, I have tested the tap and it works (NSLog) but I want to know how to launch my mapview view controller and also zoom to an x&y.
My code currently has
-(IBAction)handleMapTap:(UIGestureRecognizer *)sender {
NSLog(#"Tapped");
}
& I have tried;
MMMapViewController *mapViewController = [[MMMapViewController alloc] initWithNibName:#"MapView" bundle:nil];
[self.navigationController pushViewController:mapViewController animated:YES];
My view controller has a class set as MMMapViewController I have given the view controller a storyboard id of MapView (if it's required?).
I've read a lot of stackoverflow articles but can't seem to find an answer. If anyone can help I would be really grateful.
-(IBAction)handleMapTap:(UIGestureRecognizer *)sender
{
MMMapViewController *mapViewController = [[MMMapViewController alloc] initWithNibName:#"MapView" bundle:nil];
//[self.navigationController pushViewController:mapViewController animated:YES];
[self presentModalViewController:mapViewController animated:YES];
[mapViewController release];
}
It would probably help to know what self is, if it is indeed a UIViewController, then
I would make sure that self.navigationController is not nil.
You actually have the right code, as far as I can tell, but depending on your scenario, you could get away with presenting the mapView as a modal view controller.

How do I put the name of the IBAction pressed on my main view controller in a NSString on my modal view?

I have about ten IBActions on my main view that go to the same modal view and I need to know how to see what IBActon was pressed to get there and put that in a NSString on the modal view.
Here's the code that sends you to the modal view. The only difference in the IBActions is the name. Like playMovie: and playMovie2: and so on.
-(IBAction)playMovie:(id)sender{
VideoSubViewController *subView = [[[VideoSubViewController alloc] init] autorelease];
[subView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:subView animated:YES];}
You could create an init* on the modal view that takes that information. The usage would be:
VideoSubViewController *subView =
[[[VideoSubViewController alloc] initForAction:#"playMovie"] autorelease];
You then store that information in the view instance in order to use it.

Show NSPopover from NSToolbarItem Button

I want to show an NSPopover from an NSToolbarItem button in my toolbar.
(i.e. positioned below the button).
Ideally, I want to pass the NSView of the button to the popover to position it.
My question is, how do I get the NSView of the NSToolbarItem?
[toolbarbutton view] always returns nil.
The answer appears to be in the video for the 2011 WWDC Session 113, "Full Screen and Aqua Changes." Basically, put an NSButton inside the NSToolbaritem and use the view of that.
A blog post is here: http://www.yellowfield.co.uk/blog/?p=33, and a sample project is on github at http://github.com/tevendale/ToolbarPopover
All in the sprit of http://xkcd.com/979!
You can send the action directly from the NSButton enclosed in the NSToolbarItem (which is what you should generally do anyways, consider segmented controls, where each segment has its own target/action), and that will do the trick.
Instead of getting the view from the IBAction sender, connect an IBOutlet directly to the toolbar item and use that to get the relative view:
In your header file:
#property (weak) IBOutlet NSToolbarItem *theToolbarItem;
#property (weak) IBOutlet NSPopover *thePopover;
In your implementation file, to show the popover:
[self.thePopover showRelativeToRect:[[self.theToolbarItem view] bounds] ofView:[self.theToolbarItem view] preferredEdge:NSMinYEdge];
This will also work for showing popups from menu item selections inside a toolbar item.
While I did achieve that the Popover was shown using the approach mentioned by Stuart Tevendale, I did run into problems when I tried to validate (enable / disable) the NSToolbarItems using the NSToolbarDelegate:
-(BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem {
BOOL enable = YES;
NSString *identifier = [toolbarItem itemIdentifier];
// This does never get called because I am using a button inside a custom `NSToolbarItem`
if ([identifier isEqualToString:#"Popover"]) {
return [self someValidationMechanism];
}
// For this the validation works when I am using a standard `NSToolbarItem`
else if ([identifier isEqualToString:#"StandardToolbarItem"]){
return [self someOtherValidationMechanism];
}
return enable;
}
So I would advise not to display a Popover from NSToolbarItem. An alternative might be to show a Page Sheet: How to show a NSPanel as a sheet

dismiss UIPopoverController (if visible) after tapping a UIBarButtonItem

I'm trying to dismiss any currently visible UIPopoverControllers if/when another UIBarButtonItem is tapped as seen on Pages for the iPad when tapping between Styles/Media/Tools etc.
I've done this, but it's too specific. Looking for something generic.
if ([popoverController isPopoverVisisble]) {
[popoverController dismissPopoverAnimated:YES];
}
Thanks!
Did you set the passthroughViews property of the popover controller? If you do this, then taps outside the popover will not cause the popover to automatically dismiss, but will instead be sent to the views in that array. You should be able to add the UIBarButtonItem to this array, and then dismiss the popover in that handler.
randallmeadows answer will NOT work. UIBarButtonItem is not descendant of UIView, which means you cannot add it to passthroughViews.
The only solution I found for now is to create UIBarButtonItem with custom UIButton using
UIBarButtonItem *b = [[UIBarButtonItem alloc] initWithCustomView:button]
and then
popoverController.passthroughViews = [NSArray arrayWithObject:b.customView];
But be prepared that you'll loose all the styling - you cannot create UIButton that looks like UIBarButtoItem too easily.
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.passthroughViews = #[];
Works for me

Resources