以编程方式向UINavigationController添加按钮

时间:2014-01-20 08:59:14

标签: ios objective-c cocoa-touch

我认为这应该是一个简单的问题,但我一直在摸不着头脑。我有一个应用程序,在故事板中设置了多个ViewControllers。我希望每个视图的每个视图顶部都有相同的导航栏,但是这个导航控制器在所有视图上都有相同的按钮。导航控制器在AppDelegate中设置,按钮添加到每个ViewController的ViewDidLoad中(根据我在这里收到的另一个问题的答案),但此时,按钮(和标题)没有显示。

设置UINavigationController的位置 - AppDelegate.m:

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

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    MainViewController* mainVC = [mainStoryboard instantiateInitialViewController];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:mainVC];

    [self.window setRootViewController:navVC];

    [_window makeKeyAndVisible];

    return YES;
}

其中一个观点:

- (void)viewDidLoad
{

    [self setTitle:@"Congress app"];  // < This does not set the title of the NavController

    UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showSettings:)];
    UIBarButtonItem *showMenuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(revealMenu:)];

    //self.navigationItem.leftBarButtonItem = showMenuButton;
    //self.navigationItem.rightBarButtonItem = showSettingsButton;
    self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
    self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;

    UINavigationController *currentNav;
    UIViewController *VCname;   
    NSLog(@"First left button in navigation controller - %@", [self.navigationController.navigationItem.leftBarButtonItems objectAtIndex:0]);
    NSLog(@"First right button in navigation controller - %@", [self.navigationController.navigationItem.rightBarButtonItems objectAtIndex:0]);
    [super viewDidLoad];  
}

我有几个NSLog显示导航控制器中有什么(如果有的话),它显示:以及哪些按钮被添加但未显示?

编辑:

我刚才记得在上面的代码中有一个在VC之前触发的ViewController(InitViewController.m)。 NavigationController在这里分配:

- (void)viewDidLoad
{
[super viewDidLoad];

self.topViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
}

这很可能是问题的原因。

编辑2:

感谢Saurav Mac,我已经跟踪了问题,试图在“错误”的视图控制器上添加按钮。我在AppDelegate.m中安装了导航控制器,第一个视图控制器是InitViewController,然后调用MainViewController作为'顶视图控制器'。不敢相信我错过了,谢谢你的帮助。

5 个答案:

答案 0 :(得分:6)

您可以在公共类中创建此按钮,即NSObject的子类,然后可以在要添加的每个类中调用此方法按钮,或者您可以将此代码添加到每个类的ViewDidLoad方法中 -

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];

UIImage *img1=[UIImage imageNamed:@"image.png"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(BackButtonPressed)
  forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.leftBarButtonItem=barButton;

答案 1 :(得分:0)

self.navigationController.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationController.navigationItem.rightBarButtonItem = showSettingsButton;

将其更改为

self.navigationItem.leftBarButtonItem = showMenuButton;
self.navigationItem.rightBarButtonItem = showSettingsButton;

答案 2 :(得分:0)

    try This ...

    In AppDelegate.h

    @property (strong, nonatomic) UINavigationController *nav; //navigationControlller Object


    AppDelegate.m


    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        UIViewController* mainVC = [mainStoryboard instantiateInitialViewController];
        self.nav = [[UINavigationController alloc] initWithRootViewController:mainVC];
        [self.window setRootViewController:self.nav];
        [_window makeKeyAndVisible];
        // Override point for customization after application launch.
        return YES;
    }

    One of the Views:

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        [self setTitle:@"Congress app"];

        UIBarButtonItem *showSettingsButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showSettings:)];

        self.navigationItem.leftBarButtonItem = showSettingsButton;

      }

-(void)showSettings:(id)sender
{
    NSLog(@"showSettings");
}

答案 3 :(得分:0)

试试这个:

显示导航控制器的 leftBarButtonItem

    UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
    [face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
    self.navigationItem.leftBarButtonItem = backButton;
    [self.navigationItem setHidesBackButton:YES animated:YES];
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    [self.navigationItem setBackBarButtonItem:nil];

显示导航控制器的 rightBarButtonItem

UIImage *faceImage1= [UIImage imageNamed:@"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:@selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;

可能会帮助你。

答案 4 :(得分:0)

请试试这个......

- (void)viewDidLoad
{

     [super viewDidLoad];

     CGRect frameimgback1 = CGRectMake(0, 0, 60, 35);
     UIButton *button = [[UIButton alloc] initWithFrame:frameimgback1];
     [button addTarget:self action:@selector(back)  forControlEvents:UIControlEventTouchUpInside];
     [button setTitle:@"Ravindra" forState:UIControlStateNormal];
     UIBarButtonItem *btnL = [[UIBarButtonItem alloc] initWithCustomView:button];
     self.navigationItem.leftBarButtonItem = btnL;
}