Why is this code okay?
#interface AddViewController : UIViewController <UIPickerViewDelegate>
{
IBOutlet UILabel *category;
IBOutlet UIPickerView *categoryPicker;
}
#end
But this code is not
#interface AddViewController : UIViewController <UIPickerViewDelegate>
IBOutlet UILabel *category;
IBOutlet UIPickerView *categoryPicker;
#end
Also, I was looking up a video on how to use the UIPickerView, and instead of making properties for the IBOutlet, he just made the outlets. Which clearly has something to do with why those curly braces are there.
category and categoryPicker are instance variables, not properties. You either reference them directly via their name e.g. category or by using self->category.
Instance variables are declared in the curly braces, while properties are declared with #property
Instance variables do not have setter and getter methods, while properties have.
you need to change "IBOutlet UILabel *category;" to "#property (nonatomic, strong) IBOutlet UILabel *category;", the same for "IBOutlet UIPickerView *categoryPicker;"
Within the bracket {}, category and categoryPicker are instance variables, but if you delete the "{}", you need to use "property" so that they work the same way
Related
I have a view controller called LoginWindowViewController.h that declared a property called usernameTextField:
#property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
A string called James is associated to this TextField.
I then import another view controller,FirstViewController.h into my LoginWindowViewController.m and I also imported LoginWindowViewController.h into FirstViewController.m . In my FirstViewController.h there is a property called username.
#property (strong, nonatomic) IBOutlet NSString *userName;
Then i assign usernameTextField to username(In my FirstViewController.m). But when i NSlog the property username in my FirstViewController.m, it gives a null value.How do i fix this?
When you import classes you do not actually import any values. When you set the value of a property it is only set on that instance of the class. You will need to explicitly reference the property of your current instance to get the value you have set.
One note: IBOutlet stands for Interface Builder Outlet and is how you create a link from a storyboard or xib file UI element to a class property. So, no need to use IBOutlet if you are not linking to something in interface builder.
String should be declared
#property (nonatomic, strong) NSString *userName;
Then when you instantiate your login view controller from first view controller you can set the property like this
LoginWindowViewController *loginVC = [[LoginWindowViewController alloc] init];
[loginVC.usernameTextField setText:self.userName];
I have Class A which contains a xib with some buttons and a UITextField and Class B which contains a tableView
I want to add Class A to the table view header.
I am importing the xib from class A into class B using [[[NSBundle mainBundle] loadNibName: owner:self options:nil] objectAtIndex:0];
I just want to reflect the button and textview actions inside classB. In ClassB I do have some UITextfield Delegate methods which i expect to be called when typing some text in uitextfield defined and declared in ClassA (xib + property definition in .h file).
Class A .h:
//properties are set from xib file ClassA.xib
#interface ClassA : UIView
#property (unsafe_unretained, nonatomic) IBOutlet UIButton *addC;
#property(strong, nonatomic) IBOutlet UITextField *nText;
#end
Class B.h
#import "ClassA"
#interface ClassB : UIViewController <UITextFieldDelegate, UITableViewDataSource,...>
#property (strong, nonatomic) IBOutlet UITableView *mTableView; //property set from ClassB.xib
#property(strong, nonatomic) IBOutlet UITextField *nText_B;
#property (unsafe_unretained, nonatomic) IBOutlet UIButton *addC_B;
- (IBAction)addC:(UIButton *)sender;
#end
I don't know how to make the link ClassB button and textfield to ClassA
In ClassB viewDidLoad method i;ve tried this:
getHeader = [[ClassA alloc]init];
_addC_B = [getHeader addC];
_nText_B = [getHeader nText];
_nText_B.delegate =self;
_nText_B.text=#"";
but it's not working
I am new to iOS on this side area.
Thank you for your help
You haven't explained your problem well. Classes do not "contain xibs".
If your objective is to create a piece of user interface in XIB and reuse it, you can achieve this with [NSBundle loadNibNamed:owner:options:]. The best way to reference parts of it is through properties like Noval explained in his answer. Another possibility is to attach views to "File's Owner" outlet's properties.
you can't connecting IBOutlet that placed outside your XIB class. but if you just want to access the outlet from other class, it's possible by creating instance of ClassA in ClassB. and then accessing the property like instanceOfClassA.outletObject
Just when you think you understand something, you don't! :)
I understand that if I make a variable a property, I can access it anywhere in the Class and even set it from outside that class.
I thought if I didnt need it I could just make it an ivar. So I have a viewcontroller with about 5 UILabels. So in its viewDidLoad I say:
pharmacyName.text = self.receivedLocation.name;
pharmacyTel1.text = #"556-7843";
pharmacyTel2.text = #"991-2345";
pharmacyTel3.text = #"800-0001";
When I have declared them like so in the .h file:
#interface DetailViewController : UIViewController{
IBOutlet UILabel *pharmacyName;
IBOutlet UILabel *pharmacyTel1;
IBOutlet UILabel *pharmacyTel2;
IBOutlet UILabel *pharmacyTel3;
}
#property (nonatomic,strong) MyLocation *receivedLocation;
#end
No. Its not mandatory to create ivar as property. If you don't want to access it outside of class just use as it is. In ARC you can also declare your IBOutlet as below:
#interface DetailViewController : UIViewController{
__weak IBOutlet UILabel *pharmacyName;
__weak IBOutlet UILabel *pharmacyTel1;
__weak IBOutlet UILabel *pharmacyTel2;
__weak IBOutlet UILabel *pharmacyTel3;
}
This will keep a week reference of outlets. Here is detail of __weak and strong
There are always many ways you can approach programming tasks and standards. Our group has started using a few coding standards. We like to put our instance variables that are NOT accessed from outside the class (and protocol statements) in the private interface in the .m file like this:
#interface DetailViewController() {
NSString *value_;
}
#end
We also like to use #property for our instance ivars and declare those in the private interface as well like this:
#interface DetailViewController() {
}
#property (nonatomic, strong) IBOutlet UIlabel *pharmacyName;
#end
and then in your code, you would refer to this as self.pharmacyName. It seems to work pretty well with autocomplete, and with getting and setting. Also when you have thread safety issues, the nonatomic, strong behavior comes in handy.
What is the technical difference between them and which is the method recommended by Apple?
// 1
#interface CocoaQuizViewController : UIViewController
{
IBOutlet UILabel *myLabel;
}
#end
// 2
#interface CocoaQuizViewController : UIViewController
{
IBOutlet UILabel *myLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *myLabel;
#end
// 3
#interface CocoaQuizViewController : UIViewController
{
UILabel *myLabel;
}
#property (nonatomic, retain) IBOutlet UILabel *myLabel;
#end
// 4
#interface CocoaQuizViewController : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *myLabel;
#end
The default is (generated automatically if you drag and drop outlet directly from xib to source class):
#interface CocoaQuizViewController : UIViewController
#property (nonatomic, weak) IBOutlet UILabel *myLabel;
#end
All are quite OK.
With new LLVM you are supposed to do 4th one.
#interface CocoaQuizViewController : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *myLabel;
//even you use strong and weak intead of retain,assign,copy
#end
In earlier days, you were doing 1, 2 and 3. Now most of the thing is atomatcally done by the compiler. So your work is now easier than never before.
With New Compiler which comes with XCode4.4 and onwards gives you auto-synthesize for all the properties you declare. ivars also get created prefixed with your property name.
The 4th one, Because Now Apple has recommended all developers to make use of properties.
A couple of thoughts:
Your fourth example avoids a whole category of possible bugs that can plague the first three examples, where you can accidentally end up with two ivars (e.g. if you omitted the #synthesize, the compiler would generate an ivar called _myLabel, your myLabel ivar wouldn't be used, and, thus, would end up being redundant and only serve as a possible source of confusion).
If you use ARC (which I'd encourage, if you can), then clearly that retain reference becomes weak.
You probably shouldn't be "writing" the IBOutlet code yourself anyway. It's just an opportunity to introduce a bug. In IB, click on the "assistant editor" to show your code while working on IB, and then control-drag (or right-click-drag) from the control to the code, and IB will write your code for you! See https://stackoverflow.com/a/15551101/1271826 for screen snapshots.
For example one, I declare an object inside the interface brace {} like:
#interface testViewController : UIViewController {
IBOutlet UILabel * myLabel;
}
#property (retain, nonatomic) UILabel *myLabel;
#end
and example two, I declare an object outside the inferface brace {} like:
#interface testViewController : UIViewController {
}
#property (retain, nonatomic) IBOutlet UILabel *myLabel;
#end
I run the code and the result is the same, so I want to ask what is the different for decalare an object inside or outside the interface brace {}?
Thanks
The modern Objective-C runtimes (64-bit Mac OS X and iOS) will generate the backing store for your declared properties when you #synthesize them. So you don't need to declare them within the braces.
If you are declaring an iVar that is not a property and will only be used by the class, then they need to be declared. It's a good idea to mark these #private e.g
#interface MyClass : NSObject {
#private
NSString *privateString;
}
#property (nonatomic, copy) NSString *publicString; // be sure to #synthesize this
#end
In the second example you only declare a property. Xcode will declare object automatically.