SanitLee
SanitLee

Reputation: 1253

Set navigation item title of a view from another view that calls it

Here I have 2 views: userProfileVC and userListVC. In userProfileVC, there is a button action method:

- (IBAction)followingButtonPressed:(id)sender {
    UserListViewController *userListViewController = [[UserListViewController alloc] initWithNibName:nil bundle:nil];
    userListViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    userListViewController.navItem.title = @"Followings"; //I expect this to set userListVC navigation item title to "Followings"
    [self presentViewController:userListViewController animated:YES completion:nil];
}

This will call userListVC which I have set a property of navItem as below:

UserListVC.h

@property (weak, nonatomic) IBOutlet UINavigationItem *navItem;

UserListVC.m

@synthesize navItem;

My problem is why navigation item title in userListVC is not shown "Followings" according to what I have set in userProfileVc (see my comment in the first code) but it results nothing to the title. I don't know what I have done wrong or missing here.

Upvotes: 0

Views: 109

Answers (1)

Jef
Jef

Reputation: 4728

try changing

userListViewController.navItem.title = @"Followings"; 

in your code above to

userListViewController.title = @"Followings";

UIViewController has a (NSString *)title property. If this is not nil then this is the string you'll see in the UINavigationBar when that particular UIViewController is up front.

then change this

[self presentViewController:userListViewController animated:YES completion:nil];

to this

if (self.navigationController){
[self.navigationController presentViewController:userListViewController animated:YES completion:nil];

}else{
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:userListViewController];
    [self presentViewController:nav animated:YES completion:nil];
     }

Upvotes: 1

Related Questions