Sunday, June 9, 2013

UINavigationItem setPrompt example in Objective C (iOS).


UINavigationItem setPrompt

A single line of text displayed at the top of the navigation bar.

@property(nonatomic, copy) NSString *prompt

Discussion of [UINavigationItem setPrompt]
The default value is nil.

UINavigationItem setPrompt example.
UIView                      *viewContainingSpinner;
    UIActivityIndicatorView     *activityIndicatorView;
    UIBarButtonItem             *activityButtonItem;
    UIBarButtonItem             *rightBarButtonItem;

    // Configuring the title and the prompt title of the navigation bar
    [self.navigationItem setTitle:@"MobileMe"];
    [self.navigationItem setPrompt:@"Vérification du compte MobileMe"];

    // We will create a UIBarButtonItem that has a custom view (viewContainingSpinner).
    // A subview of viewContainingSpinner will be a UIActivityIndicatorView (activityIndicatorView)
    // We need to have this "intermediate" view to position the spinner at the right position (the UIBarButtonItem ignores the origin and height of its custom view)
    viewContainingSpinner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 85)];
    activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(20, 0, 20, 20)];
    [viewContainingSpinner addSubview:activityIndicatorView];
    [activityIndicatorView startAnimating];
    [activityIndicatorView release];

    activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:viewContainingSpinner];
    self.navigationItem.leftBarButtonItem = activityButtonItem;
    [viewContainingSpinner release];
    [activityButtonItem release];

    // Finally, configuring the right button
    rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Enregistrer" style:UIBarButtonItemStylePlain target:nil action:nil];
    [rightBarButtonItem setEnabled:NO];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    [rightBarButtonItem release];

Example of [UINavigationItem setPrompt].
UINavigationBar *bar = picker.navigationBar;
UINavigationItem *navItem = bar.topItem;
navItem.prompt = @"Some new prompt";

UINavigationItem setPrompt example.
self.navigationItem.prompt = NSLocalizedString(@\"Please select the appropriate media type:\", @\"Page Five Prompt\");

End of UINavigationItem setPrompt example article.