简体   繁体   English

如何在iPhone中为UILabel提供输入

[英]how can i give input for UILabel in iPhone

i have 2 classes . 我有2节课。 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. 我导入class1并创建像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. 我检查了( NSLog print)类,该方法将被调用,但输入不会到来。我检查了其标签也连接到我的类。因为我当时在viewDidLoad中输入了相同的输入,这时它可以正常工作。

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

guide me where i'm doing wrong. 指导我哪里做错了。

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. 设置这些值的正确方法是使用在整个Cocoa框架中使用的MVC模式。 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. 我写这样一个例子, DelegationExample ,在不久前,其显示了如何在第一视图中的文本字段是通过使用委托协议的第二视图控制器一个文本框的值填充。 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 我已将链接更新到带有ARC和Storyboards的iOS6新项目

Take one NSString variable in AppDelegate class and synthesize it properly. 在AppDelegate类中获取一个NSString变量,并将其正确合成。 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. 谢谢。

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. 我想你明白了。

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 Aadhira是正确的,当您在class2中创建class1的新实例时,这是错误的,您必须获取class1的原始实例,这可以通过创建一个静态函数来实现,该函数返回class1的当前实例,如下所示

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM