简体   繁体   English

在另一个UIViewController中从UiViewController更改UILabel文本

[英]Change UILabel Text from UiViewController in another UIViewController

Okay it seems like I have made some mistakes and I did not get it with pointer and initializations by now... 好的,看来我犯了一些错误,并且到目前为止我还没有通过指针和初始化来获得它...

Here is the problem : 这是问题所在:

I have a UIViewController for a registration process called : RegisterViewController 我有一个用于注册过程的UIViewController:RegisterViewController

It calls a method in its ViewDidLoad : 它在其ViewDidLoad中调用一个方法:

[self performSelector:@selector(activateUsernamePopover) withObject:nil afterDelay:0.1];

This method looks like this : 此方法如下所示:

- (void) activateUsernamePopover {
PopoverViewController *popcontroller = [[PopoverViewController alloc] init];

popcontroller.title = nil;

[popcontroller setPopoverText:@"Test"];

FPPopoverController *popover = [[FPPopoverController alloc] initWithViewController:popcontroller];

popover.arrowDirection = FPPopoverArrowDirectionUp;

popover.border = NO;
popover.tint = MgoGreyTint;

[popover setShadowsHidden:true];

[popover presentPopoverFromView:_usernameInput]; }

This will made a Popover visible. 这将使Popover可见。 This works great. 这很好。

But I Do have a few more TextFields where I want to show a Popover with a different text. 但是我确实还有一些TextField,我想在其中显示带有不同文本的Popover。

So I made a method in the PopoverViewController called setPopoverText : 因此,我在PopoverViewController中创建了一个名为setPopoverText的方法:

- (void)setPopoverText:(NSString *)text {
[_popoverLabel setText:text];
[_popoverLabel setNeedsDisplay]; }

I call it in my activateUsernamePopover method : 我在activateUsernamePopover方法中调用它:

[popcontroller setPopoverText:@"Test"];

And there is the problem. 还有问题。

I can log the text in the PopoverViewControllers method setPopoverText its fine. 我可以在PopoverViewControllers方法setPopoverText方法中记录文本。

But it did not change the text. 但是它并没有改变文本。 I logged the _popoverLabel like this : 我这样记录_popoverLabel:

NSLog(@"%@",_popoverLabel);

and its (null). 及其(空)。

I know there is some issue with the pointer or the instance of PopoverViewController I am working with, but objective c is not that clear to me yet. 我知道我正在使用的指针或PopoverViewController实例存在一些问题,但是对我来说目标c尚不清楚。

Anyone got some answers for me ? 有人给我一些答案吗?

How can I change the Text of that UILabel ? 如何更改该UILabel的文本?

I also could imagine giving the Text to the Controller while instancing it. 我还可以想象在实例化时将文本提供给Controller。

Something like that : 像这样:

PopoverViewController *popcontroller = [[PopoverViewController alloc] initWithPopoverText:@"Test"];

But I don´t know how. 但是我不知道如何。 I don´t need to change the Text while the popover is visible. 弹出窗口可见时,我不需要更改文本。 It will be released when the user taps in the TextField or elsewhere. 当用户点击TextField或其他位置时,它将被释放。

Thanks so far. 到目前为止谢谢。

Since the UILabel is not created yet when you call init method. 由于调用init方法时尚未创建UILabel。 the way to do it is to keep text in the NSString property. 这样做的方法是将文本保留在NSString属性中。

In you PopoverViewController, create the init method like this 在您的PopoverViewController中,创建如下的init方法

@interface ViewController : UIViewController
- (id)initWithPopoverText:(NSString *)text;
@end

In the implementation file, keep hold of the text in the property and on viewDidLoad, you could set the text to the label. 在实现文件中,保留属性中的文本,并在viewDidLoad上,可以将文本设置为标签。

@interface PopoverViewController ()
    @property (nonatomic) NSString *popoverText;
@end

@implement PopoverViewController
- (id)initWithPopoverText:(NSString *)text {
    self = [super init];
    if (self) {
        _popoverText = text;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //set label.text here
    self.popoverLabel.text = self.popoverText;
}
@end

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

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