How to Hide button using identifier (Restoration ID) - iphone

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.

Related

Calling a function of UIViewController from a child UIView

I have a UIViewcontroller on which I have added a UIView as a subview and then added another UIView as another subview over the first view.
Now I want to call a method in the UIViewController from the last UIView. All of these views are custom views and have been created as different classes.
What would be the best way to call this topmost view from the child of the child?
You cannot add a view as a UIViewController's subview. I believe you added it as a subview of the controller's view. I do not understand what you mean to ask in the question. Please correct me if I'm wrong.
I assume you want to call a method in the UIViewController on some user interaction. If this view (the child of child)is a button, you could simply do this:
[button addTarget:nil action:#selector(methodToCall:) forControlEvents:UIControlEventTouchUpInside];
A nil target will result in the method methodToCall in the UIViewController to be called.
If you don't see a call there, make sure the view hierarchy (all predecessor views in it) is user interaction enabled.
A better approach (If you haven't subclassed UIButton) would be to create a protocol for your custom views, specify a tap gesture recognizer, specify a delegate and send call a method in the delegate whenever you receive a tap.
The whole point of a view controller is that it's in control of the view. You shouldn't be adding knowledge to the view which requires it to know to pass information to the controller. The view should either have UIControls on it and the controller sets the appropriate target and action or the controller should add gestures to the view and, again, specify appropriate target and action.
I can not get what you want exactly..if you want simple topmost view then you set tag of this view when you call addsubview .and every time increment tag when add subview and decrement tag when remove subview.then you easily get topmost view with help of current tag.
Do like this,
In TopView.h
#property (nonatomic, assign) YourViewController *ViewController_object;
In TopView.h
-(void)dealloc {
[super dealloc];
ViewController_object=nil;
}
-(void)your_method {
[ViewController_object method_from_VC];
}
hope it will helps you...

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

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

Reference to UIButton inside a view

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

programmatically ActionSheet's Buttons

I need some guidance for following :
I have an ActionSheet. When it appears it shows my custom view with two buttons.
The View is called from another (.h/.m)file using xib.
How can I set cancel event on one of the button. Buttons are added into view not in ActionSheet.
OR How can I put buttons on ActionSheet programmatically ?
Thanks...
Here is the link to your answer.
Also you can refere the Class Reference for UIActionsheet to get all the solutions related to action sheet.
You can make your own action sheet. For this you have to make a view controller and you can use it as action sheet by using
[self presentModalViewController:YourViewController animated:YES];
And on cancel btn action you can do
[self dismissModalViewControllerAnimated:YES];
Hope it will work for you.
You have to remove that view from superview when user tap on cancel button.