2010-05-17 146 views
80

嗨我需要在導航欄中以編程方式在右側設置按鈕,這樣如果我按下按鈕,我將執行一些操作。我用編程方式創建了導航欄;以編程方式將按鈕添加到導航欄

navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ]; 

同樣,我需要添加按鈕,在此導航欄的右側。爲此,我使用的,

1.  

UIView* container = [[UIView alloc] init]; 

    // create a button and add it to the container 
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)]; 
    [container addSubview:button]; 
    [button release]; 

    // add another button 
    button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)]; 
    [container addSubview:button]; 
    [button release]; 

    // now create a Bar button item 
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container]; 

    // set the nav bar's right button item 
    self.navigationItem.rightBarButtonItem = item; 
    [item release]; 

2.  

UIImage *im; 
    im=[UIImage imageNamed:@"back.png"]; 
    [button setImage:im forState:UIControlStateNormal]; 
    [im release]; 
    backButton = [[UIBarButtonItem alloc] initWithCustomView:button];  
    [backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)]; 
    [self.navigationItem setRightBarButtonItem:backButton]; 

3.  

UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button"    style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)]; 
    self.navigationItem.rightBarButtonItem = refreshItem; 

    [refreshItem release]; 

我嘗試了所有這些方法,但沒有一個在右側顯示按鈕。

回答

173

裏面我UIViewController派生類中,我使用的內部viewDidLoad如下:

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
           initWithTitle:@"Flip"            
           style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(flipView:)]; 
self.navigationItem.rightBarButtonItem = flipButton; 
[flipButton release]; 

這增加了一個按鈕,右邊的標題翻轉,它調用的方法:

-(IBAction)flipView

這看起來非常像你#3,但它在我的代碼中工作。

+0

我需要一個冒號添加到選擇 - 「行動:@選擇(flipView :)]; – Rydell 2012-11-06 17:03:14

5
self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)]autorelease]; 

-(void)saveAction:(UIBarButtonItem *)sender{ 

//perform your action 

} 
+1

它似乎沒有工作,爲什麼這可能是任何線索 – 2012-11-30 06:43:53

3

使用下面的代碼:

UIBarButtonItem *customBtn=[[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(customBtnPressed)]; 
[self.navigationItem setRightBarButtonItem:customBtn]; 
2

簡單使用本地editBarButton這樣

self.navigationItem.rightBarButtonItem = self.editButtonItem; 
[self.navigationItem.rightBarButtonItem setAction:@selector(editBarBtnPressed)]; 

然後

- (void)editBarBtnPressed { 
    if ([infoTable isEditing]) { 
     [self.editButtonItem setTitle:@"Edit"]; 
     [infoTable setEditing:NO animated:YES]; 
    } 
    else { 
     [self.editButtonItem setTitle:@"Done"]; 
     [infoTable setEditing:YES animated:YES]; 
    } 
} 

玩得開心...!

19
UIImage* image3 = [UIImage imageNamed:@"back_button.png"]; 
CGRect frameimg = CGRectMake(15,5, 25,25); 

UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg]; 
[someButton setBackgroundImage:image3 forState:UIControlStateNormal]; 
[someButton addTarget:self action:@selector(Back_btn:) 
    forControlEvents:UIControlEventTouchUpInside]; 
[someButton setShowsTouchWhenHighlighted:YES]; 

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton]; 
self.navigationItem.leftBarButtonItem =mailbutton; 
[someButton release]; 

/////稱爲事件

-(IBAction)Back_btn:(id)sender 
{ 
    //Your code here 
} 
+0

完美!謝謝 – TheMan 2014-08-17 06:10:29

+0

哇,這是一個很酷的答案..也完美.. – 2016-04-01 12:28:43

3

在ViewController.m

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(back)]; 

[self.navigationItem setLeftBarButtonItem:cancel]; 
的viewDidLoad方法

「(背面)」選擇是dissmiss的方法當前ViewController

-1

大家好!我使用UIIMagePicker創建了兩個UIInterface方向的問題的解決方案。在我的ViewController中,我處理segue到UIImagePickerController

**我使用了..

-(void) editButtonPressed:(id)sender { 
    BOOL editPressed = YES; 
    NSUserDefaults *boolDefaults = [NSUserDefaults standardUserDefaults]; 
    [boolDefaults setBool:editPressed forKey:@"boolKey"]; 
    [boolDefaults synchronize]; 

    [self performSegueWithIdentifier:@"photoSegue" sender:nil]; 

} 

**

然後在AppDelegate類我這樣做。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 

    BOOL appDelBool; 
    NSUserDefaults *boolDefaults = [NSUserDefaults standardUserDefaults]; 
    appDelBool = [boolDefaults boolForKey:@"boolKey"]; 

     if (appDelBool == YES) 
      return (UIInterfaceOrientationMaskPortrait); 
     else 
      return UIInterfaceOrientationMaskLandscapeLeft; 
} 
+0

對不起這是什麼問題! :P – Shad 2015-02-17 19:38:01

+0

請仔細閱讀問題/題目小夥子們...... !!!! – Tuhin 2015-12-02 11:41:17

8

要添加搜索按鈕導航欄上使用此代碼:

UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toggleSearch:)]; 
self.navigationController.navigationBar.topItem.rightBarButtonItem = searchButton; 

並實現以下方法:

- (IBAction)toggleSearch:(id)sender 
{ 
    // do something or handle Search Button Action. 
} 
+1

'topItem'是什麼使它爲我工作 – JSmyth 2016-04-10 16:30:48

+0

非常感謝您的解決方案。 topItem是我錯過的東西 – 2017-12-18 07:26:35

5

斯威夫特版本,在viewDidLoad中添加此:

var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "doneButton:") 
navigationItem.rightBarButtonItem = doneButton 

而這在你身上R查看控制器類

func doneButton(sender: UIBarButtonItem) { 
    println(111) 
} 
6

如何添加按鈕在迅速增加導航欄:

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onAdd:") 

使用onAdd:

func onAdd(sender: AnyObject) { 
} 
2

嘗試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; 

    -(void)SaveButtonClicked 
    { 
    // Add save button code. 
    } 
0

如果你是不是在找一個BarButtonItem但的導航欄,然後下面簡單的按鈕代碼工作:

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[aButton setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forState:UIControlStateNormal]; 
[aButton addTarget:self 
      action:@selector(showButtonView:) 
    forControlEvents:UIControlEventTouchUpInside]; 
aButton.frame = CGRectMake(260.0, 10.0, 30.0, 30.0); 
[self.navigationController.navigationBar addSubview:aButton]; 
相關問題