通过segue将NSString值分配给UI标签文本

时间:2016-03-26 20:07:05

标签: ios objective-c iphone nsstring

所以我想要一个字符串@“Welcome%@!”被标记为我的指针*名称,因此如果用户在UI标签文本上输入名称,则可以将其标记到下一页,并且还可以将“欢迎”问候语附加到名称上。我的代码如下:

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"setName:"]) {
    if ([segue.destinationViewController respondsToSelector:@selector(setName:)]) {
        NSString *name = self.nameTextField.text;
        [segue.destinationViewController performSelector:@selector
         (setName:) withObject:name];
    }
}
}
Next Page “Second View Controller.m” :

@ interface SecondViewController()     @property(弱,非原子)IBOutlet UILabel * nameLabel;     @implementation SecondViewController

-(void)setName: (NSString *)name
{
    _name = name;
    self.nameLabel.text = self.name;

}

任何人都可以告诉我如何输入一个字符串,以便它与(NSString *)名称一起被标记

谢谢

1 个答案:

答案 0 :(得分:1)

了解Formatting String Objects。以下是我的回答:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"setName:"]) {
        if ([segue.destinationViewController respondsToSelector:@selector(setName:)]) {
            NSString *name = self.nameTextField.text;
            SecondViewController *secondCtrl = [segue destinationViewController];
            [secondCtrl setName:name];
        }
    }
}

在SecondViewController中:

@interface SecondViewController () 
@property (weak, nonatomic) IBOutlet UILabel *nameLabel; 
@end

@implementation SecondViewController

- (void)setName:(NSString *)name
{
    _name = name;
    self.nameLabel.text = [NSString stringWithFormat:@"Welcome %@", self.name];
}

@end