how to customize bar button but keep the segue? - iphone

I have added a bar button item in my navigation bar and linked a segue from the button to another view controller.
Now I tried to custmize the look of this button by
UIButton *mapButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
[mapButton setImage:[UIImage imageNamed:#"map.png"] forState:(UIControlStateNormal)];
[mapButton setShowsTouchWhenHighlighted:YES];
[showAllButton setCustomView:mapButton];
The segue is no longer being called when I tap on it. What went wrong?
Thanks!
Leo

You can manually make a call to perform the segue, like this:
[self performSegueWithIdentifier:#"pushDeviceDetail" sender:self];
So if you set a target and selector on your button, and call that within the selector, you should be able to perform the segue.
Just remember to name your segue in interface builder to be able to do this, and then include that identifier in the call you make.

well, for one thing, you don't add a target to this new mapButton.
Not familiar with storyboards but I'm sure this new button doesn't work if you don't add a target and selector...

Related

MVC - button action in view or controller?

Using MVC. I have a button. Button drawing is in view. The button action should be in controller, right? How do I add action to the button? From view, no reference should be kept of controller (for MVC), then how to set action.
Should the button be made public and accessed by controller?
Or, button should be drawn in controller. (not good)
Or, use a delegate (but then every view-controller pair in MVC will have to have a delegate)
Create an IBAction in the header of your controller:
-(IBAction)buttonPressed:(id)sender;
Now in Interface Builder wire up your button to this action (use CTRL drag from button to files owner).
Next in the implementation of your controller you can write the code that executes when the button is pressed in -(void)buttonPressed:(id)sender
maybe you can make the button is a property of your view. or you can make a public method of your view like -(void)setupButtonSelector:(SEL)selector; the selector is your buttons selector.
button should be accessible by the controller so that it can set an action.
For example you could do it a little like it works with UITableCells :) --> you could have a property button on your view
BUT
this is a matter for debate and also depends on personal taste
Got it. Has to set addTarget:nil. The action will be passed to next responder in chain. In this case view controller.
Set a Target Selector on button which you are using in view. And Define a method with selector method name.
Ex:
[button addTarget:self action:#selector(YourMethodName) forControlEvents:UIControlEventTouchUpInside]; // Declare this where you are adding button in view.
-(void)YourMethodName // Use this where you need to perform that button action.
{
// Do what you need.
}
And if you are adding the view which contains that button at runtime.Try another trick.
Set a Tag Value for that button while adding button.And in Your controller access that button via its Tag value and add target on button.
Ex;
UIButton *btn = (UIButton*)[self.view viewWithTag:10];
[btn addTarget:self action:#selector(methodName) forControlEvents:UIControlEventTouchUpInside];

Where is UIButtonTypeInfoLight?

I'm looking for the UIButtonTypeInfoLight choice in the identifier list for button bar items, and I'm not seeing it. So, two questions.
Is it just missing and I'll have to create it manually, in code? I'm wondering why they would omit it.
Assuming I do have to create the button manually, to call a segue from it,do I need to manually perform the segue rather than using the storyboard?
I'm assuming I'd do this [self performSegueWithIdentifier:#"ShowChecklist" sender:nil]; from the method that my button is calling.
The code that created my button is
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(infoButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
So, Is this a common pattern? Am I doing something weird and nonstandard? It just seems to me to be weird that if my BarButton were just a custom button that said "info" I could create it and wire it up all in the Storyboard... but for this one, I have to doit all in code.
UIButtonTypeInfoLight is a UIButton. Button bar items are UIBarButtonItems. They have nothing to do with each other. You're just confusing apples and oranges.
However, a UIBarButtonItem can contain a UIButton. (That's merely by virtue of the fact that it can contain any UIView.) So drag a UIButton into a toolbar. What you get is a UIBarButtonItem containing a UIButton. If you double-click the UIBarButtonItem, you get the UIButton. Now you can set the button's type.

iPhone: Is it possible to have an 'info'-button in all views in my app?

As mentioned in the title, how do I implement such a functionality?
I am using the code below, (in my viewDidLoad), to get the button on my Navigation Controller of my main view.
UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(viewWillAppear:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Not sure how I can get it displayed in all my views.
Each view pushed to the navigation stack has an own navigation bar. So I afraid you have to add this button to each navigation bar, when you create a view and before you push this view to the navigation stack.
You could simply add an info button to the window, instead of creating a new button for each view controller. Since the window never changes, the button will always be on screen. You only have to take care that no other views are on top of the button.
I would suggest you create an base view controller, and add the bar button to your navigationItem in viewDidLoad. Then all your view controllers subclass from the base controller, rather than the default UIViewController. This should solve your problem.

What happens when back button is pressed in navigationBar

I wonder what is the function is called when the back button is pressed on the navigationBar.
I want to add some functionality when the button is pressed, who knows it?
Thanks in advance
The functionality you want is in the UINavigationBarDelegate protocol. Implement the -navigationBar:shouldPopItem: method and set your class as the delegate of the navigation bar in question.
Assuming you're referring to native controls, there's no way to do quite what you want, just using the built-in stuff. What you want to do is create a 'fake' back button, and stick it up in the left side of the navigation bar. Then you can set its target and action to whatever you like.
I suppose you're talking about the back button that automatically is added to a UINavigationBar when you push a new viewcontroller on a navigationcontroller.
The default action for the back button is to pop the current viewcontroller from the navigation stack and return to the previous viewcontroller.
If you want to define a custom behaviour to the backbutton you'll have to create a new button and tie a selector to it's action property:
//Create a new barbutton with an action
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleDone target:self action:#selector(doSomething)];
// and put the button in the nav bar
self.navigationItem.leftBarButtonItem = barbutton;
[barbutton release];
Edit:
//An example on how the doSomething method could be implemented.
-(void) doSomething
{
//Do your custom behaviour here..
//Go back to the previous viewcontroller
[self.navigationController popViewControllerAnimated:YES];
}

iPhone SDK: Advancing from one view to another using a button tap

I want to make a really simple iphone app: one screen with a single button... when the button is tapped a new screen appears. That's it. No animations, nothing,
I've tried endlessly to make the NavBar sample project do this... and it works but only if I use a UINavigationController with a table that I can tap etc. I've tried all the skeleton projects in XCode too.
I thought I was done when I did this:
[[self navigationController] presentModalViewController:myViewController animated:YES];
But I couldn't do it without the UINavigationController. I just want a simple example.
Thanks so much!
One way you could do this is to create a new UIView and then when the button is pressed add that new UIVIew as a subview, therefore making it what you see.
If you make the new view its own subclass of UIView you would do something like this.
LoginView *login = [[LoginView alloc] initWithFrame: rect];
[mainView addSubview: login];
[self presentModalViewController:myViewController animated:NO];
Will pop up a new view, no animations, nothing. To get rid of it, inside myViewController:
[self dismissModalViewControllerAnimated:NO];
Though I reccomend you use the nice sliding animations (change NO to YES.) And yes, you can stack them up. I think this is better than creating a new UIView, but I may be wrong.
The correct way to do this is set up your project with a UINavigationController. In your root view controller, add your button in the view controllers's view. Then in viewDidLoad, register for UIControlEventTouchUpInside events from you button. Then, in your event callback, call:
[self.navigationController pushViewController:[[[SecondViewControllerClass alloc] initWithNib:nibName bundle:nil] autorelease]];
What kdbdallas suggested will work, but you won't get the nice sliding effects, nor will the navigation bar automatically change and provide your users with a back button.