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];
Related
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.
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];
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...
I loaded an UIView out of a nib-file. I wrote some methods which I was able to connect to the First Responder of the nib. They work fine.
Now I have to reference a UIButton which is embedded in the loaded view. But since it's no view controller / just an UIView (I guess?), the File's Owner does not detect my IBOutlet UIButton * button in the .h-file.
So what's going on here? Thanks!
there are a couple of ways to do this.. one of them is..
Set the buttons tag property in interface builder, then in code, loop through all the subviews looking for the view with the tag you set
..Didn't test this, but it should be something like
for (UIView *subView in [view subviews]) {
if (subView.tag == <YOUR TAG HERE>) {
UIButton *button = (UIButton *)subView;
}
}
..I'd wrap this in a function called getSubviewWithTag so you can reuse it elsewhere
When you drag connections within Interface Builder, you don't always have to connect to Files Owner. Drag from your UIButton, and drop on the UIView that contains it. If it has an IBOutlet UIButton property on you custom UIView class - it will connect up.
Here's a sample of how it can be done:
Right-click the UIButton in the xib, click on the referencing dot and drag it to File's Owner. As soon as you release the click, a popup appears of all the IBOutlets you can connect the element with.
EDIT- If it doesn't turn blue, then you haven't set the Custom Class for your File's Owner! You can do that as below. Click on File's Owner and then the 3rd tab. Set it to your custom view controller.
It would be easy to alloc and subview your button in that specific view.
Then define the action dynamically in the .m class of that "View" may that will work for you.
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
I want to enable or disable the button. Is there any way to drag the outlet of UIButton on to the Bar Button? I tried, I could do that. How to enable or disable the button if I can't connection them together?
Thanks!
You might use the UIBarButtonItem.