AsimRazaKhan
AsimRazaKhan

Reputation: 2202

Setting a property in another VC by using presentViewController method

I am trying to set a .text properties in another viewController class but it does not get set. how can I achieve this result without using a segue?

AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
AccountTypeVC.serviceTitle.text = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];

The .h of AccountTypeVC

@property (strong, nonatomic) IBOutlet UILabel *serviceTitle;

Upvotes: 0

Views: 70

Answers (1)

Bilal
Bilal

Reputation: 19156

It's not necessary that after instantiateViewControllerWithIdentifier all the UI elements are initialised. Recommended is to bind all the UI elements with data once the viewDidLoad of the view controller has been called.

Add another property in AccountTypeVC.

@property (strong, nonatomic) NSString *serviceTitleText;

And set it to label in viewDidLoad method.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.serviceTitle.text = self. serviceTitleText;
}

And set serviceTitleText property instead of label before presenting the AccountTypeVC.

AccountTypeVC *vc = (AccountTypeVC *)[mainStoryboard instantiateViewControllerWithIdentifier:@"AccountTypeVC"];
vc.serviceTitleText = [tableData objectAtIndex:indexPath.row];
[self presentViewController:vc animated:NO completion:nil];

Upvotes: 4

Related Questions