2014-02-19 40 views
8

我有一個UIBarButtonItem並希望以編程方式設置前一個控制器的操作(在我的情況下,我以前的視圖是UITableViewController)。回覆按鈕以編程方式轉到上一個視圖

下面是我的代碼,目前我正在使用它來製作條形按鈕項目,但該按鈕還沒有轉到上一個視圖。

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                   style:UIBarButtonItemStyleDone target:nil action:nil]; 
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Website"]; 
item.leftBarButtonItem = leftButton; 
item.hidesBackButton = YES; 
[myBar pushNavigationItem:item animated:NO]; 
+0

你的線顯示選擇器在哪裏? –

+0

哎呀,你在這裏 –

+0

UINavigationBar * myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,64)]; [self.view addSubview:myBar]; UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithTitle:@「Done」 style:UIBarButtonItemStyleDone target:nil action:nil]; [leftButton addTarget:self action:@selector(method) –

回答

14

添加以下的控制器代碼,在- (void)viewDidLoad功能:

呼叫[self addBackButtonWithTitle:@"back"];如果您想自定義標題爲返回按鈕。

[self addBackButtonWithImageName:@"back_button_image_name"];如果您想自定義帶圖像的後退按鈕。

/** 
* @brief set lef bar button with custom title 
*/ 
- (void)addBackButtonWithTitle:(NSString *)title { 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed:)]; 
    self.navigationItem.leftBarButtonItem = backButton; 
} 

/** 
* @brief set left bar button with custom image (or custom view) 
*/ 
- (void)addBackButtonWithImageName:(NSString *)imageName { 
    // init your custom button, or your custom view 
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    backButton.frame = CGRectMake(0, 0, 40, 22); // custom frame 
    [backButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; 
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    // set left barButtonItem with custom view 
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 
} 

- (void)backButtonPressed:(id)sender { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
9

你可以在你的viewDidLoad方法寫:

UIBarButtonItem *btn=[[UIBarButtonItem alloc] initWithTitle:@"Back"  style:UIBarButtonItemStyleBordered target:self action:@selector(btnClick)]; 
self.navigationItem.leftBarButtonItem=btn; 

,然後使用此:

// call of method 
-(void)btnClick 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
3

[自dismissViewControllerAnimated:YES完成:零]

相關問題