I have a beginner's question.
I have a view with a button, which switches to another view by a segue.
In that case, must this button be declared in ViewController.h, like this:
#property (weak, nonatomic) IBOutlet UIButton *calculate;
or is it optionnal?
I remark that the segue transition works without button declaration.
Thanks in advance !
Short answer: No, you do not need to connect the button to an IBOutlet.
Not-so-short-answer: You only need to create an IBOutlet for objects that you want to reference from code. Such as if you wanted to change the Title on the button for some condition. This would have no effect on your Segue connection.
The declaration is optional if you don't want to access the instance of the button in your code (of course, you can still access it with viewWithTag or by getting the subviews of the main view, but it's much cleaner with a declaration).
You don‘t need an outlet here.
The IBOutlet doesn‘t declare the button. The button is created by the XIB or Storyboard file. An Outlet is simply a name under which the XIB/Storyboard-loading mechanism will store the address of the button so you can modify its title or whatever.
If you create a button yourself, you have its address, but if the button is created by the XIB, you need some other way to get individual objects‘ addresses out. That‘s what an IBOutlet is for.
Related
I want to have a couple of buttons in my iOS app at the bottom of the screen and so used a UIToolBar and added a couple of Bar Button Items to it. But I realized that these BarButtonItems are not considered to be regular buttons. So I am not able to set actions on mouse up on these. Is there a way to set a function to be called when these buttons are clicked? If this is not possible through the storyboard, can I access it programmatically?
Select the UIBarButtonItem, Control+Dragging at the implementation section in .m file. Create an IBAction. You may put a NSLog statement to check the user tap.
Do not make an IBAction from the bar button item object or the code will not run.
In the document outline, control+drag from the button INSIDE the bar button item container. The IBAction should run normally now.
Create one outlet:
#property (weak, nonatomic) IBOutlet UIBarButtonItem *menuBarBtn;
In ViewDidLoad or somewhere you can add like this:
[_menuBarBtn setAction: #selector( revealToggle:)];
In order to simplify my question please consider following scenario.
FirstViewController has a UILable which is "connected" as IBOutlet from storyBoard.
SecondViewController which is a subclass of FirstViewController also has a UILable, but has one more UIButton. It has a separate represent in the storyBoard.
I don't know whether it is correct or not.
I ctr+drag the UILable from the viewController of the SecondViewController to the IBOutlet in the FirstViewController.m file.
Working like champ, the association is correct for me. But I still don't know whether it is the correct way to do so. And will it have any bad effect to my viewContorller.
Thanks
I am unable to set a referencing outlet for a text field I have created. I am trying to do this via Ctrl-Drag. Can anyone advise? I have set the delegate as the View Controller. In general, I only have one view which is populated with a few text fields and I have written the following code for the ViewController.h which is what I'm trying to set up as a Referencing Outlet:
#property (weak, nonatomic) IBOutlet UITextField *userIDText;
First, make sure the control you're dragging from is the same class as the outlet (in this case, UITextField) or it could be a subclass of that class.
Second, make sure control's parent view is a subclass of the file you're dragging to.
Third, make sure the file you're dragging to is saved. This is the most common problem I run into. Go to the source file you're dragging to and push Cmd+S to save.
Fourth, when in doubt, Cmd+Shift+K to clean, Cmd+B to build, then try again. If that still doesn't work, restart Xcode. And if it's still not working, go back through the checklist.
In my case, to be able to create a referencing outlet,
I had to open the Storyboard.
Click on the top View Controller element (left panel in the Storyboard)
In the Identity Inspector (right panel), I had to select from the list my custom view controller class before I could drag and create the referencing outlet in it.
In my cace,(swift3)
1.right click on the storyboard.
2.Ctrl-Drag from black window and ReferencingOutlet to View on the storyboard.
enter image description here
I am currently trying to implement a custom radio button class that I have been messing with (located at https://github.com/t4ku/RadioButtonWithUIKit). This radio button solution draws everything programmatically.
I was wondering if there is a similar solution using buttons that were added via Storyboard. I already have my design exactly how I want it through Storyboard and would much rather somehow "link" the buttons with my radio button class.
Any newer ideas or suggestions would be greatly appreciated.
You can change the class of an object on Storyboard in the identity inspector (you can manually enter the class instead of relying on the drop down menu). You can then link the object however you like.
If Storyboard doesnt accept the RadioButton class for some reason you could also create a custom class of UIButton that would inherit from the RadioButton class and then use that in the same way.
That control is based on UIView, so in your storyboard just add a new View object, select it, and then set the class (at the top of the image) to RadioButton:
You won't be able to see anything but a blank view where you place it, but it will work properly when you launch the application. This will at least make it easier to resize, position, etc.
If I understand your question right, to refer to a button added to a storyboard programmatically, you have to link the variable name in your .h file to the actual button in your .xib file. If you add the following line to your .h file
#property (nonatomic) IBOutlet UIButton *theButton;
Then when you return to the .xib, there should a yellow cube (file's owner) to the left of the storyboard panel. If you right click on that cube, you should get a list of outlets. At the end of the line that has your button's name, there should a circle. If you Ctrl+Click and Hold and drag it to the button in the storyboard, it should like the variable in the .h file to the button in the storyboard.
So now when you refer to the variable name you created, you will be referring to the button in the storyboard.
in my app I've implemented this toolbar within Storyboard:
As you can see, I've used four buttons and three flexible space bar button items.
For each button I created an outlet and the corresponding action and I've linked it all with Storyboard. Everything works well.
Now I want to dynamically replace a button of the toolbar.
I know that, if the toolbar itself was implemented programmatically, this can be easily achieved with the following:
[toolbarButtons replaceObjectAtIndex:4 withObject:newButton];
I'm wondering if this is possible also in my case, where toolbar and buttons are implemented via Storyboards. Any ideas?
Thanks in advance,
yassa
If your UIToolbar is part of a UINavigationController then you can access it directly with toolbarItems property. Otherwise you would have to create a IBOutlet in your viewController and link your toolbar to it to access it.
This would be the case whether you were using a storyboard or NIB.
If you've created outlets, Use the Outlet property to change it.
self.myToolBarButton = newButton;
where "myToolBarButton" is the name you gave that button.