Cannot connect Segmented Control to its IBOutlet - iphone

I have created a segmented control in the interface builder.
In my ViewController.h:
#interface ViewController : UIViewController <MKMapViewDelegate>
#property IBOutlet UISegmentedControl *Segment;
- (IBAction)switchMode:(id)sender;
#end
What I could do was to connect the Segmented Control with the IBAction but I cannot connect it with the IBOutlet!

Add a segmented control to the nib/Storyboard
Add the following code into the .h
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
In your storyboard or xib make sure that the files owner has the same classname as that of the class in which you written the outlet
Right click on the segmantControl and a window with outlets and actions appears
click and drag on the referencing outlet and drop it on the filesowner a new pop appears which includes your outlet written in code select it .
Connection established

you have forgotten to write parameters of property, correct it like below code
#property(nonatomic,retain) IBOutlet UISegmentedControl *Segment;
after this synthesize this property in .m file like this
#Synthesize Segment;

It appears that with some xCode update you can no longer connect certain outlets to your .h. You should be fine connecting it in your .m though:
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *mySegmentedController;
#end
I will read some docs and see when this changed. Also there really is no reason to connect this property to your public interface (hence why it is no longer allowed). Only the View Controller of that class should have control over it.

Related

Declaration of a button process

I would like to understand the process of the declaration of a button.
#interface MerdaViewController : UIViewController{
IBOutlet UIButton *button;
}
#property (retain, nonatomic) IBOutlet UIButton *button;
#end
Why do we need those steps?
Is it always required?
Thank you so much.
Alex.
To create a button outlet you just need this line.
#property (retain, nonatomic) IBOutlet UIButton *button;
To create button action
-(IBAction)doSomething:(id)sender;
Just like to add another point:
Outlets should generally be weak/assign, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong/retain
at least in relatively new versions of XCode, you don't need to declare that instance variable. It's automatically taken care of.
you need the #property so you can call self.button and do whatever you like to it. IBOutlet tells XCode it's an element in your interface builder, so you can hook it up with that visual button you added to IB.
more often than not I find myself actually declaring an -(IBAction)buttonAction:(id)sender; rather than an IBOutlet. This way when you tap that button, the IBAction method is automatically triggered.

iOS - Connect UILabel from interface builder to code

I want to set a custom font on a UILabel, which I've figured out how to do just fine. My question is how do I access a UILabel that I created via the drag and drop interface builder in the code?
I've tried ctrl + clicking the UILabel and dragging it to the file's owner but it won't let me. I also tried opening the assistant editor and connecting the UILabel directly to the corresponding .h file, but that won't work either. How do I access the UILabel programatically?? I know this should be really easy.
// YourController.h
#interface YourController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *theLabel;
#end
// YourController.m
#implementation
#synthesize theLabel;
///....
#end
Now with the IBOutlet marker in your code you should be able to drag from File's Owner to the label (not the other way round) in IB.
Assuming you added the UILabel to your .xib file. ctrl click the UILabel and dragged it to your .h file. You will have to enter certain info including the name of the Label. Use the name of the label that you entered from y our .m file to access it.
By ctrl+click and dragging the UILabel from the .xib to the .h, you will basically add the UILabel as a property. It will automatically added implementation of it in the .m file so you'll be good to go.
If you want to modify the UILabel at any point, you need to declare it in the .h file, and the .m file where you plan to modify it.
When you declare it is should like roughly like:
#interface MyObject {
UILabel *_testLabel;
}
#property (nonatomic, strong) IBOutlet UILabel *testLabel
Then of course you would do,
#synthesize testObeject = _testLabel;
in your .m file. After you've done this, you can link the actual variable to your physical label in your .xib file the way you were attempting to before.

UIWebView cannot be linked to my outlet !

I'm modifying my program and I cannot link the Outlet to the xib.
The code below is from the header file :
#interface GraphNavController : UINavigationController {
...
IBOutlet UIWebView *webView;
...
}
...
#property (nonatomic, retain) IBOutlet UIWebView *webView;
...
and in the .m file :
#implementation GraphNavController
#synthetize webView;
This image is captured when I tried to assign the outlet to the webview :
If you have any suggestion to solve my problem, you're reaaaaaaaly welcome :-)
Thanks!
Right click on "File's Owner" to see the outlet and then you can connect it.
Also, make sure that your File Owner class is GraphNavController
It seems that your IBOutlet is not shown and thats why you are not able to connect it.
Try this one:
Go to Interface Builder, File >> Read Class File
The select the file in which you have create the IBOutlets
Hope this will help....

Add subview not working?

I have two xib files:
MainView.xib and DetailView.xib
Both are controlled by MainViewController. MainView.xib loads when the app first opens, but if a user clicks on a button, the app loads DetailView.xib as a subview.
DetailView should load because I made an IBOutlet in the MainViewController to the view in the DetailView.xib file.
I am trying to use the addSubview command, but for some reason it is not actually executing the command. It will go through the command, but nothing will actually change. Here is the command:
[self.view addSubview:myDetailView]
where myDetailView is the IBOutlet
What is wrong with this setup?
Thanks for the help.
EDIT:
MainViewController.h (left generated code out):
IBOutlet UIView *myDetailView;
#property (nonatomic, retain) IBOutlet UIView *myDetailView;
MainViewController.m:
#synthesize myDetailView;
NSLog myDetailView before you add the subview, and if it returns "(null)" then the myDetailView has not been initialised. Make sure that you have connected the view in Interface Builder.
I bet myDetailView is nil.
It may be an IBOutlet, but it has to be connected. And the XIB in which you connected the IBOutlet should obviously be loaded.
How did you load the DetailView.xib in your code? Did you use loadNibNamed:owner:options:?

iPhone Development - multiple views in one window.

I want to have a UIView at the top half of my app, and a UITable at the bottom half. If I create a basic window application I can do this using IB. My main problem is that I seem to be only to do this in the delegate file which I would like to avoid.
I repeat the exact same steps I did in delegate in a separate controller and all I get is a blank screen.
0#interface RootViewController : UIWindow {
UIWindow *myWindow;
UIView *headerView;
UITableView *tableView;
}
#property (nonatomic, retain) IBOutlet UIView *headerView;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) IBOutlet UIWindow *myWindow;
#end
I have each of these connect to the right view in rootviewcontroller.xib
I was wondering if anyone had any advice / snippets / examples I could use?
You need to use both the delegate file and Interface Builder. Interface Builder makes a view that binds to your delegate through IBOutlets and IBActions. I would run through several iPhone tutorials found on google before continuing.
Your RootViewController certainly shouldn't be a subclass of UIWindow. You probably want UIViewController for that.