Answer to Stackoverflow question 2: Unable to set the title of a view controller

Unable to set the title of UINavigation Controller 

- In order to set the title, you need to set the title of the view controller. For this you need to have a navigation controller, and set its title. Then you can hide the navigation controller, if you wish. I have pasted the sample code below.

- Once you set the title of the navigation controller, then you can push the views however you like. See the sample code.


.h file declarations

@property (strong, nonatomic) UINavigationController *navigationController;

.m app delegate implementation

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.navigationController = [[UINavigationController alloc] init];

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

[self.navigationController pushViewController:self.viewController animated:YES];

// Override point for customization after application launch.

self.window.rootViewController = self.navigationController;

[self.window makeKeyAndVisible];

return YES;

}

// loading a view based on login

/*

* Callback for session changes.

*/

- (void)sessionStateChanged:(FBSession *)session

state:(FBSessionState) state

error:(NSError *)error

{

switch (state) {

case FBSessionStateOpen:

if (!error) {

// We have a valid session

NSLog(@"User session found");

//UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

testViewController *tv = [[testViewController alloc] init];

[self.navigationController pushViewController:tv animated:YES];

self.navigationController.navigationBarHidden = NO;

//navigationController.navigationItem.backBarButtonItem = backButton;

//[self.window setRootViewController:self.navigationController];

}

break;

case FBSessionStateClosed:

case FBSessionStateClosedLoginFailed:

[FBSession.activeSession closeAndClearTokenInformation];

break;

default:

break;

}

[[NSNotificationCenter defaultCenter]

postNotificationName:FBSessionStateChangedNotification

object:session];

if (error) {

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@"Error"

message:error.localizedDescription

delegate:nil

cancelButtonTitle:@"OK"

otherButtonTitles:nil];

[alertView show];

}

}

// code to hide the navigation bar in the base view controller

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"title";

self.navigationController.navigationBarHidden = YES;

}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s