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];
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.
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...
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];
I have custom buttons in the nav bar, I want to toggle their state by am confounded by their accessors.
I know to get the button of a view (slightly different code than showed below) or cell I can do this:
mybutton=(UIButton*)[cell.contentView viewWithTag:5];
How can I access the buttons of my navigation bar? (they are already set in code)
cheers
Robert
When you create the button and add it to the navbar instead of releasing the button retain it. Then you can reference them whenever you need to.
So add a property to the view controller:
#property (nonatomic, retain) UIButton *myButton;
Then in the method where you create the button use:
self.myButton = [UIButton ...
Remember to release the myButton in the view controller's dealloc method.
I have created a UIBarButton item in Interface Builder and linked it to a UIBarButton item property in the class. In Interface Builder it's Style = Plain and Identifier = Custom and it's Title is blank.
Inside the class file on the viewDidLoad method I am trying to add a custom view to this UIBarButtonItem property.
E.g
UISegmentedControl *newButton = [[UISegmentedControl alloc] initWithItems:....];
newButton.momentary = YES;
newButton.segmentedControlStyle = UISegmentedControlStyleBar;
newButton.tintColor = [UIColor .....];
[self.myBarButtonItem setCustomView:newButton];
and this results in NOTHING showing up at all. Why is that?
I've read that if I create the UIBarButtonItem programmatically:
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];
and then add this BarButtonItem to the toolbar - it would work. My issue is I have lots of stuff going on my toolbar and this UIBarButton item needs to be on the FAR right of the toolbar and if I can't link to it directly in interface builder, then I'd have to build out ALL my toolbar items programmatically to get the layout I need going.
So is there a way to do this with a customView when linking to a UIBarButtonItem created in IB?
Thanks!
To change stuff that's on a UIToolBar you use it's items property. That should answer your question : How to programmatically replace UIToolBar items built in IB
Edit:
The way i like to work with those, is make a bunch of IBOutlets in the view controller class for each uibarbuttonitem. In Interface Builder, i put the first (default) items to be on the toolbar normally as a toolbar subview and all that, and the rest of them i put as top level items in the xib (same level as the view that is connected to the view controller's .view property outlet). That way i don't have to programatically create them. And still have them hidden to later attach them to the uitolbar.
If you use this approach than don't forget to call release on the top level IBOutlets if you use ivar IBOutlets (as opposed to #property IBOutlets) as all top level objects in a xib that aren't connected to a KVC compilant reference.