is it right way in obj-c programming - objective-c

i wonder if the way i programm is right way or not. can you help me?
#interface ViewController : UIViewController
{
UILabel *messageLabel;
}
#end
when i declare new object in .h and create in .m i have to use #property (nonatomic, retain) UILabel *messageLabel? i saw few listings where when object are created by code #property doesn't exist and few where does exist and i'm confused.
is it correct when i don't use #property (in example for UILabel, UIImageView, UIButton) when i create objects by code?

#property is a key word useful to create setter/getter methods automatically for that field. If you don't need to access your label from outside your view controller you won't need to use #property and your code is fine.

Your code is correct. #property is used globally throughout your app, rather than being specific to your UIViewController. If you are only planning on referring or changing the label/other component define it within the { } otherwise #property should be used

Related

.h instance variable declaration

I'm having a hard time understanding why the following textfield is declared twice in some tutorials.
In the .h file:
# include <UIKit/UIKit.h>
#interface MyViewController : UIViewController {
UITextField *name; // <----- What do I need this for? Is it the same as below?
}
#property (nonatomic, strong) IBOutlet UITextField *name; // <----- Same as this?
#end
At first I thought this would be something like an instance variable, but they are only declared here in the .m file, right?
.m file
#import "MyViewController.h"
#implementation UIViewController {
NSString *myString; // <----- This is an instance variable, right?
}
What's the "UITextField *name;" for? Don't I only need the second one with the #property in front? Thank you.
This is an old way, just use property is OK.
If you declare both, you must use #synthesize name; in your .m file to make self.name same as name.
XCode4.2 auto synthesize name = _name. So use self.name as much as possible in your .m file.
Variable in {} just use for internal or private, when you don't want implement setter and getter.
If you are targeting iPhone OS or 64-bit Mac OS X then you do not need to define ivars for your properties. Take a look at Dynamic ivars: solving a fragile base class problem

Difference in setting up a class?

I may not have worded the question right, but I am not sure if what I am asking makes 100% so here goes:-)
In Xcode you can set a #class (name of class) above the #interface in the header file.
Is this the same as changing the the UIViewController in the name of the class? See code below:
So is this the same -
#class CoreDataClass;
#interface FlipsideViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) CoreDataClass *cdc;
As this:
#interface FlipsideViewController : CoreDataClass <UITableViewDataSource, UITableViewDelegate>
{
}
//This file declares the UITableView
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#end
??
If this is not the same, how is it different and what are advantages to the different implementation?
The Difference is only really asked if they are similar:-)
#class is not used to create a class, but to forward declare another one. See this question for a good explanation.
They are not the same at all. The first case is a 'forward declaration' - you are telling the compiler that the class CoreDataClass exists, so that you can refer to it in your header file without actually importing the files that define it.
The second case, you are declaring that FlipsideViewController is a subclass of CoreDataClass, and inherits all its methods and instance variables.
They're not even related. The difference is that the superclass ("parent" class) of your view controller will be different (and this can lead to nice unrecognized selector errors...). Forward-declaring a class using the #class keyword is just a convenient way of referring to a class when one doesn't want to import a whole framework header hierarch just in order to refer to one class. I. e., if you don't need to know anyting about a class except that it exists, you can use this keyword. Be careful, however, if you maks heavy use of the class - in those cases, the class forward-declaration is not considered a good solution.
In first case when you use #class it's inform XCode that you will be using CoreDataClass somewhere and you will #import header for example in .m file, in second case you're inherit from CoreDataClass (you will get access to all public and protected properties)

issue using access privileges

I have two view controllers and nibs. I populated one view controller with a toggle switch and declared this in its header file:
#public UISwitch *toggleSwitch;
and exposed it as a property like this:
#property (nonatomic,retain) IBOutlet UISwitch *toggleSwitch;
I also connected the switch with toggleSwitch outlet. Now I want to use this toggleSwitch field in my other view controller, how do I do that? Isn't using #public in the field declaration enough? Please help. Thank you.
No problem at all. Just use the switch like this:
vcWhereYouDeclaredTheSwitch.toggleSwitch.on = YES;
or
BOOL test = [vcWhereYouDeclaredTheSwitch.toggleSwitch isOn];
inside your other view controller.
Here are some general thoughts about propertys:
Memory management : Behind the scenes it will create a setter which creates the variable with correct memory management. It will save you some headaches because you can easily see how the memory management is done (strong/weak and retain/copy/assign).
Accessibility from other classes: if you declare your #property in the .h and #synthesize it in the .m you ivar will be public readable and writeable. You can prevent this with a privat class extension. You even can declare a #property public readonly and declare them internally readwrite via a privat class extension.
Eg: a private property
// [In the implementation file]
#interface MyClass ()
#property (nonatomic, retain) NSMutableArray* someData; // private!!
#end
#implementation MyClass #synthesize someData
#end
Custom getter and setter: If you like you can still write custom getter and setters and you can even just write a getter or setter and let the other one automatically #synthesize. And you can write custom logic into such a getter and setter e.g. you can reload a tableview after a #property has changed.
Automatic Key-Value-Observing (KVO) compliant: If you use or planning to use KVO you get it basically for free by just declaring the property. Nothing else need to be done!
If you need you iVar to be public it is simpler to write one #property than writing a getter and setter for a iVar
With a #property you do not need to declare in iVar (in iOS and 64bit Mac Os X applications). You can do it via the #synthesize:
#synthesize myiVar = _myIvar;
You have made the property of UISwitch. So, you can use it anywhere by using the viewcontroller object.
Suppose you wanna use it in the view where you are currently then use it
self.toggleSwitch
// or
viewControllerObject.toggleSwitch

variable accessing

I have a variable x in one class.And I want to access the updated x value in some other class.
There is so much of confusion.Can I use property?.Please help me.
Thanks in advance
Do you mean that you want to be told when the value changes? Have a look at Key Value Observing
To simply access an iVar in one class from another, a property is exactly what you want.
The syntax is :
in your .h
#interface myclass : NSObject {
UIWindow *window;
}
#property (nonatomic, retain) UIWindow *window;
#end
in your .m
#implementation myclass
#synthesize window;
...
#end
The #synthesize directive instructs the compiler to produce a lot of boilerplate code (as directed by the (nonatomic, retain) specifiers. In this case to handle thread safety and memory management.
Also note that in Objective-C 2.0 the iVar declaration UIWindow *window; is not required.
If you want to be notified in your second class when an iVar is updated, you need to look at key value observing. Unless you are writing a framework or some very dynamic code, that is probably overkill.
Maybe this tutorial will help you out..
If this is not what you mean, please rephrase the question, because i don't understand it..
Edit: Or a shared Instance can be used
you could access it by #import Classname, and then just use the getter that is created with the property. but first initialize the class you have imported..
#import "ClassY.h"
#implementation ClassX
ClassY * classY;
NSString * name;
...
name = [classY name];
...
#end

Cocoa / Objective-C: Access a (Boolean) variable in different Classes

Situation:
Noob / Xcode 3.1
I have an AppView (NSView subclass) and an AppController (NSObject subclass)
in AppView.h i declare a boolean (BOOL: booleanDraw), which i set to 'NO' in AppView.m
When a button is clicked it 'launches' an action (AppController .h/.m) now i want to change booleanDraw to YES when the button is clicked.
I searched and found: do it with #property okay i tried to do that but it didnt work. (because i didnt totally get what to do probably)
i did:
#property BOOL booleanDraw;
(in AppView.h)
#implementation AppView
#synthesize(readwrite, nonatomic) booleanDraw;
(in AppView.m)
AppView *obj;
obj.booleanDraw = YES; // implicitly calls [obj setVar:3]
(in AppController.m)
Thanks for any help, i read some tutorials already but often they suggest some steps that should be basic but that dont belong to my repertoire, and the ADN often confuse me xD sorry but believe me im trying^^
You just reversed the synthesize and property statements:
in .h:
#property (nonatomic) booleanDraw;
(by default properties are readwrite, you only need to state when they are readonly)
in .m:
#synthesize booleanDraw;
In the controller you need to get the app view reference, the code you posted would not work unless you set "obj" to something.