MVC - button action in view or controller? - iphone

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];

Related

How to Hide button using identifier (Restoration ID)

I added a button to a view using storyboard(not programatically) and i want to hide that button by calling a function. Is it possible to hide that button programatically using identifier..
--Thanx
can't I use something similar to this: if(button.identifier isEqualToString:#"btnMyButton")
Simplest way is to make use of the tag property of the button. In INterface builder set the tag value of the button.
if(button.tag == Button_tag_value)
{
button.hidden = YES;
}
Use this code:
yourButton.hidden = YES;
Hope this helps!
you would need to create an IBOutlet for the button. You can then use the IBOutlet to hide it.
You can add a tag to the button in the IB. Tag works as an identifier in hierarchy of views. In your view controller, on you self.view (assuming the button is subview of parent view) object you can send viewWithTag message with the Tag of the button you added in IB. This will return you the UIButton object and using the hidden property, you can hide it.
This does not require any IB connection of IBAction to be defined.

Back button has been created in AppDelegate, how to use it for back action?

I have created back button in AppDelegete. Also I have created a navigation controller (customized). On that navigation bar this back button appears. So it appears on all of my views. But i dont know how to give back action for that, so When I press "back" button it should go to the previous page.
Create this method in your AppDelegete
-(void)gotoBack:(UIViewController*)pViewController
{
[pViewController.navigationController popViewControllerAnimated:YES];
}
and call gotoback method in your controller like this
[AppDelegete gotoBack:self];
if only wants to navigate back, Add this target to your navigation back button and no need to create navigation back method, it will call navigation controller’s popViewController selector
[_backButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

iOS - UIButton's text is getting set back to nib set value when pressing

I have a UIButton that has an initial value that I have set in interface builder. When pressing this button, I show a modal view controller which, after some user interaction, passes a value back to the first controller. I then update the button's text with this value that was passed back.
All this works fine, however, if I press this button again, as the modal view controller is animating on screen, the button which I pressed has the original text that it had when the view first loaded. Does anyone know why this is and how to make it retain the new text that I set on it?
Thanks in advance.
When you set the title of the button make sure to set it for UIControlStateNormal and UIControlStateHighlighted
[button setTitle:#"YourNewTitle" forState:UIControlStateNormal];
[button setTitle:#"YourNewTitle" forState:UIControlStateHighlighted];

Enable/Disable Interface Builder uibutton programmatically

In my mapkit app, I would be able to enable/disable programmatically buttons created via Interface Builder. The idea is enable a one or more button if a annotatios is selected, and disable if not. for example, in my, an action:
-(void)traceRoute:(id)sender{
//trace route between user location and annotation selected
}
is defined in order to trace route between user location and annotation mapkit. In IB, defined a button and linked to that action, it works. But I do not understand how I can enable a button not defined programactically but in the interface builder. Any help is apreciated!
try this one
-(void)traceRoute:(id)sender{
UIButton *button = (UIButton *)sender;
[button setEnable:YES];
}
IN the IB we do have option of enabled. And if we want to enable/disable it according to the conditions, then we need to create the outlet of button and handle it programmatically.
You need to define that button in the view controller interface:
IBOutlet UIButton* myButton;
Connect this outlet to your button in the IB, then you can do:
[myButton setEnabled:YES];

how to customize bar button but keep the segue?

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...