質問

右のバーボタンアイテムをナビゲーションバーに追加したいので、クリック時に特定の機能を実行します。

正しいバーボタンアイテムを追加するために次のコードを作成しましたが、それが完了した後、バーボタンアイテムがナビゲーションバーに表示されていません。

-(void)viewDidload{
    self.navigationItem.rightBarButtonItem = 
    [[[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemAdd                                                                                                     
                                target:self
                                action:@selector(Add:)] autorelease];    
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}
役に立ちましたか?

解決

-(void)viewDidLoad
{ 
    [super viewDidLoad];
    app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add:)] autorelease];
}

-(IBAction)Add:(id)sender
{
    TAddNewJourney *j=[[TAddNewJourney alloc]init];
    [app.navigationController pushViewController:j animated:YES];
    [j release];
}

他の答えを試してください。この回答を投稿して、viewcontrollerに問題だと思うナビゲーションコントローラーがない場合に機能するようにしました。

他のヒント

このようなuiviewcontrollerを持っていると仮定しましょう。

UIViewController *vc = [UIViewController new];

そして、あなたはそれをナビゲーションコントローラーに追加しました:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

このようにして、RightBarbuttonitemはそうします 表示されていません:

nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

しかし、このようにして表示されます:

vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"xyz" style:UIBarButtonItemStyleDone target:self action:@selector(xyz)];

NavigationControllerの代わりに独自のViewControllerを使用して、NavigationItemを参照してください。

このコードをviewdidloadに追加します

UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:@"Calculate" style:UIBarButtonItemStylePlain target:self action:@selector(nextview)];
self.navigationItem.rightBarButtonItem=chkmanuaaly;
[chkmanuaaly release];

ここでの回答のほとんどは、uinavigationbarのRightbarbuttonitemを新しいVC内から表示した後に設定します。 Jánosによって実際に答えられた私の必要性は、Calling UiviewControllerからuinavigationItemアイテムを設定することです。したがって、次のコード(ありがとう、János)。

//内部から(別名通話コントローラー):

    UIViewController *c = [[[UIViewController alloc] init...] autorelease];
    c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissModalViewControllerAnimated:)] autorelease];
    c.navigationItem.title = @"Title Goes Here";
    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
    nav.navigationBarHidden = FALSE;
    [self presentModalViewController:nav animated:YES];

さて、これはJánosの答えに冗長に見えるかもしれませんが、彼の応答をマークアップするために、より多くの担当者ポイントが必要です。 ;-)

私はそれがより完全な応答になると思います、そしてそれはどんな状況でも役立つはずです:

-(void)viewDidLoad{

    //Set Navigation Bar
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];

    //Set title if needed
    UINavigationItem * navTitle = [[UINavigationItem alloc] init];
    navTitle.title = @"Title";

    //Here you create info button and customize it
    UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];

    //Add selector to info button
    [tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];

    //In this case your button will be on the right side
    navTitle.rightBarButtonItem = infoButton;

    //Add NavigationBar on main view
    navBar.items = @[navTitle];
    [self.view addSubview:navBar];

}
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];

次のコードを使用します。

    UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addUser)];
    self.navigationItem.rightBarButtonItem=add;
    [add release];

これがあなたを助けることを願っています。

楽しみ!

UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);

[dButton addTarget:self  action:@selector(clickdButton:)
  forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:@"iconnavbar.png"]
         forState:UIControlStateNormal];    
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];

UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;

その後:

-(IBAction)clickdButton:(id)sender{
    classexample *tempController=[[classexample alloc] init];
    [self.navigationController pushViewController:tempController animated:YES];
    [tempController autorelease];
}

迅速:

let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
                                  style: .plain,
                                 target: self,
                                 action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)

func settingsButtonTapped() {
    // Implement action 
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top