0
votes

So I am trying to have a string @"Welcome%@!" be tagged to my pointer *name so if a user inputs there name on the UI Label text it can be segued to the next page and also have the greeting "Welcome" be attached to the name. My code goes as follows:

@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 (weak, nonatomic) IBOutlet UILabel *nameLabel; @implementation SecondViewController

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

}

Can anyone show me how to input a string so it will be tagged along with the (NSString *)name

Thank you

1

1 Answers

1
votes

Learn about Formatting String Objects. Below is my answer:

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

In your 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