how can i give input for UILabel in iPhone

核能气质少年 提交于 2019-12-11 18:01:54

问题


i have 2 classes . in my first class i have one label. now i have to give input for that label from my second class. here is my code.

 IBOutlet UILabel *label1;

@property(nonatomic, retain) IBOutlet UILabel *label1;

@synthesize label1;

I call this label like this. I import my class1 and create object like classone. I checked(NSLog print)class and that method will be called but that input won't come.i checked that label its also connected to my class.because i give same input in my viewDidLoad that time its working fine.

 NSString *ram= @":13123123312";
        classone.label1.text= ram;

guide me where i'm doing wrong.


回答1:


Setting values of previous views is trickier than this. What it the view has been removed by the OS under memory pressure?

The proper way of setting these values is to use the MVC pattern that is used throughout the Cocoa frameworks. Your second view controller sets a property of the previous view controller. And when the previous view needs to be shown, it takes its value from this property.

The usual way to correctly hook up a view controller to talk back to a another view controller lower in the stack is to use a delegate protocol.

I wrote an example of this, DelegationExample,a while ago which shows how a textfield in the first view is populated by a textfield's value in the second view controller using a delegate protocol. You might find it useful to see how I have done this as an example.

Update

I've updated the link to a new project for iOS6 with ARC and Storyboards




回答2:


Take one NSString variable in AppDelegate class and synthesize it properly. And store the value of the second class's variable to that like:

appDelegate.strLbl = [NSString stringWithformat:@"%@",strVal];

and then copy that value to the label in first class like:

lblVal.text = [NSString stringWithformat:@"%@",appDelegate.strLbl];

Hope that helps you. Thanks.




回答3:


Actually you should have the reference of the first class in the second class. You should not allocate a new instance. If you create new instance, then the instance for which you have set the label value is different from the actual one which you will see on clicking back.

I guess you got this.




回答4:


Aadhira was right, when u create a new instance of class1 in class2 its wrong, You have to get the original instance of class1, this can be achieved by creating a static function which returns current instance of class1, as shown below

static  classone*  sInstance;

@implementation classone

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    sInstance = self;
    return self;
}

+(classone*) getInstance {
   // NSAssert (sInstance!=nil, @"classone:getInstance: called when singleton was not initialized!");
    return sInstance;
}


来源:https://stackoverflow.com/questions/8575227/how-can-i-give-input-for-uilabel-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!