如何添加右键,一个UINavigationController?(How to add a righ

2019-06-17 22:57发布

我试图刷新按钮添加到导航控制器没有成功的最上面一栏。

这里是标题:

@interface PropertyViewController : UINavigationController {

}

这里是我在尝试添加:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain
                                          target:self action:@selector(refreshPropertyList:)];      
        self.navigationItem.rightBarButtonItem = anotherButton;
    }
    return self;
}

Answer 1:

尝试在viewDidLoad中这样做。 一般来说,你应该反正推迟任何你能到这一点,当一个UIViewController是inited它仍然可能是相当长一段时间它会显示之前,早做工作,占用内存是没有意义的。

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
  self.navigationItem.rightBarButtonItem = anotherButton;
  // exclude the following in ARC projects...
  [anotherButton release];
}

至于为什么它没有目前的工作,我不能100%肯定地说,没有看到更多的代码,但很多事情发生init和视图之间的负载,并且你可能会做一些使navigationItem来重置之间。



Answer 2:

尝试添加该按钮是将要推到这个视图控制器的navigationItem PropertyViewController你已经创建的类。

那是:

MainViewController *vc = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
vc.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];

PropertyViewController *navController = [[PropertyViewController alloc] initWithRootViewController:vc];

现在,已通过编程创建此infoButton将在导航栏中显示。 这个想法是,导航控制器拾取来自其显示信息(标题,按钮等) UIViewController它即将显示量。 其实你不直接添加按钮和这样的UINavigationController



Answer 3:

看来,有些人(比如我)可以来这里寻找如何在界面生成器添加一个导航栏按钮。 下面的回答显示了如何做到这一点。

添加导航控制器到你的故事板

选择您的视图控制器,然后在Xcode的菜单中选择编辑>嵌入>导航控制器

或者,你可以添加一个UINavigationBar从对象库。

添加栏按钮项目

一拖UIBarButtonItem从对象库顶部导航栏。

它应该是这样的:

设置的属性

你可以双击“项目”来改变文字的东西,如“刷新”,但对于刷新一个实际的图标,你可以使用。 只需选择属性检查器UIBarButtonItem系统项目选择刷新

这会给你的默认刷新图标。

添加IB行动

从控制拖动UIBarButtonItem的视图控制器添加@IBAction

class ViewController: UIViewController {

    @IBAction func refreshBarButtonItemTap(sender: UIBarButtonItem) {

        print("How refreshing!")
    }

}

而已。



Answer 4:

没有为“刷新”默认的系统按钮:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *refreshButton = [[[UIBarButtonItem alloc] 
                            initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                            target:self action:@selector(refreshClicked:)] autorelease];
    self.navigationItem.rightBarButtonItem = refreshButton;

}

- (IBAction)refreshClicked:(id)sender {

}


Answer 5:

您可以使用此:

Objective-C的

UIBarButtonItem *rightSideOptionButton = [[UIBarButtonItem alloc] initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(rightSideOptionButtonClicked:)];          
self.navigationItem.rightBarButtonItem = rightSideOptionButton;

迅速

let rightSideOptionButton = UIBarButtonItem()
rightSideOptionButton.title = "Right"
self.navigationItem.rightBarButtonItem = rightSideOptionButton


Answer 6:

-(void) viewWillAppear:(BOOL)animated
{

    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnRight setFrame:CGRectMake(0, 0, 30, 44)];
    [btnRight setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [btnRight addTarget:self action:@selector(saveData) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
    [barBtnRight setTintColor:[UIColor whiteColor]];
    [[[self tabBarController] navigationItem] setRightBarButtonItem:barBtnRight];

}


Answer 7:

对于SWIFT 2:

self.title = "Your Title"

var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))

var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("yourMethod"))

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton


Answer 8:

以下是在夫特溶液(根据需要设置的选项):

var optionButton = UIBarButtonItem()
optionButton.title = "Settings"
//optionButton.action = something (put your action here)
self.navigationItem.rightBarButtonItem = optionButton


Answer 9:

为什么你的子类UINavigationController ? 有没有需要继承它,如果你需要做的就是添加一个按钮即可。

建立一个分层结构与UINavigationController顶部,然后在你的根视图控制器的viewDidLoad:方法:设置按钮,并通过调用其连接到导航项目

[[self navigationItem] setRightBarButtonItem:myBarButtonItem];


Answer 10:

你可以试试

self.navigationBar.topItem.rightBarButtonItem = anotherButton;


Answer 11:

斯威夫特4:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "tap me", style: .plain, target: self, action: #selector(onButtonTap))
}

@objc func onButtonTap() {
    print("you tapped me !?")
}


Answer 12:

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 50)];
view.backgroundColor = [UIColor clearColor];

UIButton *settingsButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[settingsButton setImage:[UIImage imageNamed:@"settings_icon_png.png"] forState:UIControlStateNormal];
[settingsButton addTarget:self action:@selector(logOutClicked) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setFrame:CGRectMake(40,5,32,32)];
[view addSubview:settingsButton];

UIButton *filterButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[filterButton setImage:[UIImage imageNamed:@"filter.png"] forState:UIControlStateNormal];
[filterButton addTarget:self action:@selector(openActionSheet) forControlEvents:UIControlEventTouchUpInside];
[filterButton setFrame:CGRectMake(80,5,32,32)];
[view addSubview:filterButton];



self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];


Answer 13:

尝试对我来说this.It工作。

导航栏也添加背景图片右边的按钮。

 UIBarButtonItem *Savebtn=[[UIBarButtonItem alloc]initWithImage:[[UIImage    
 imageNamed:@"bt_save.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 
 style:UIBarButtonItemStylePlain target:self action:@selector(SaveButtonClicked)];
 self.navigationItem.rightBarButtonItem=Savebtn;


Answer 14:

使用右按钮导航栏的代码与你赢了标题和调用的方法点击右键后。

UIBarButtonItem *btnSort=[[UIBarButtonItem alloc]initWithTitle:@"right" style:UIBarButtonItemStylePlain target:self action:@selector(sortedDataCalled)];
   self.navigationItem.rightBarButtonItem=btnSort;
}

-(void)sortedDataCalled {
    NSLog(@"callBtn");    
}


Answer 15:

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


Answer 16:

- (void)viewWillAppear:(BOOL)animated
{    
    [self setDetailViewNavigationBar];    
}
-(void)setDetailViewNavigationBar
{
    self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
    [self setNavigationBarRightButton];
    [self setNavigationBarBackButton];    
}
-(void)setNavigationBarBackButton// using custom button 
{
   UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"  Back " style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeftButton:)];          
   self.navigationItem.leftBarButtonItem = leftButton;    
}
- (void)onClickLeftButton:(id)sender 
{
   NSLog(@"onClickLeftButton");        
}
-(void)setNavigationBarRightButton
{

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(onClickrighttButton:)];          
self.navigationItem.rightBarButtonItem = anotherButton;   

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


Answer 17:

    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshData)];



}

-(void)refreshData{
    progressHud= [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    [progressHud setLabelText:@"拼命加载中..."];
    [self loadNetwork];
}


Answer 18:

你应该添加barButtonItem - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated方法。



Answer 19:

只需复制并粘贴此Objective-C代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addRightBarButtonItem];
}
- (void) addRightBarButtonItem {
    UIButton *btnAddContact = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [btnAddContact addTarget:self action:@selector(addCustomerPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btnAddContact];
    self.navigationItem.rightBarButtonItem = barButton;
}

#pragma mark - UIButton
- (IBAction)addCustomerPressed:(id)sender {
// Your right button pressed event
}


Answer 20:

如果我们删除该视图控制器或尝试添加界面生成器(main.storyboard)内新的视图控制器可能出现此问题。 要解决这个问题,它需要添加“导航项目”中新的视图控制器。 有时它发生,我们创造新的视图控制器屏幕上,并没有连接到“导航项目”自动。

  1. 转至main.storyboard。
  2. 选择新的视图控制器。
  3. 转至文档大纲。
  4. 检查视图控制器的内容。
  5. 如果新视图控制器不具有导航项,然后,从以前的视图控制器复制导航项目,将其粘贴到新的视图控制器。
  6. 保存和清理项目。


Answer 21:

你也可以添加使用多个按钮rightBarButtonItems

-(void)viewDidLoad{

    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD1:)];
    UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD2:)];

    self.navigationItem.rightBarButtonItems = @[button1, button2];
}


Answer 22:

@Artilheiro:如果它是一个navigationbased项目,U可以创建BaseViewController。 所有其他视图将继承这个基本视角。 在基本视点ü可以定义通用的方法来添加右键或修改左边的按钮文本。

例如:

@interface BaseController:{的UIViewController

} - (无效)setBackButtonCaption:(的NSString *)标题;

(空隙)setRightButtonCaption:(的NSString *)标题selectot:(SEL)选择器;

@end //在BaseView.M

(空隙)setBackButtonCaption:(的NSString *)标题{

UIBarButtonItem *backButton =[[UIBarButtonItem alloc] init];

backButton.title= caption;
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

} - (无效)setRightButtonCaption:(的NSString *)标题selectot:(SEL)选择{

  UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init]; 
rightButton.title = caption;

rightButton.target= self;

[rightButton setAction:selector];

self.navigationItem.rightBarButtonItem= rightButton;

[rightButton release];

}

现在,在任何自定义视图,实现这个基本视图调用的方法:

@interface LoginView:BaseController {

在一些方法调用基方法为:

SEL SEL = @selector(switchToForgotPIN);

[超级setRightButtonCaption:@ “忘记PIN” selectot:SEL];



文章来源: How to add a right button to a UINavigationController?