开发者

Can I pass/access primitive data types (float,double,int) from another view?

I have two classes of View Controllers....

View 1

View 2

View 1

@interface BillDataEntryViewController : UIViewController {

    double x;
    double y;

//It is a big file but only showing which is necessary..

}

//I have not declare any Properties nor I have synthesize it?


View 2

In another View I am creating an Object of view1 called objView1

but I am not开发者_开发问答 able to access objView1.double ? Why..


You will need to add properties for each ivar and synthesize them. Then you can call: objView1.x or objView1.y

@property (nonatomic, assign) double y;


retain cant be given for primitive types. but u can synthesize them.


You should read up on objective-c's properties. Specifically the modifiers that you can give when specifying them in a .h file. However, as a rule of thumb :

  1. For objects that you want to keep a reference to use retain - this will call retain on each object that you give it (it will also call release on the previous one for you!)

    @property (nonatomic, retain) UIView *view;
    
  2. For primitive data types use assign - this will just set your variable to the value you give it :

    @property (nonatomic, assign) float myFloat;
    
  3. For things that have a mutable subclass use copy :

    @property (nonatomic, copy) NSString *myString;
    

    Option (3) is for things like NSString, NSData, NSURL, NSSet, NDictionary etc - basically anything that has a Mutable version (i.e. NSString has an NSMutableString).

  4. readonly if you don't want anyone to be able to change your data - this works for both pointers and primitive types.

    @property (nonatomic, readonly) double myDouble;
    @property (nonatomic, readonly) UIView *myView;
    


In my opinion, passing values between two views is a bad idea. You should use a ViewController for that.

Moreover, you should make the vars accessible to other objects by creating getters (either by writing methods or by declaring ans synthesizing properties).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜