2014-01-10 59 views
0

我想創建一個後退按鈕,我的iPhone應用程序,我想回到我的登陸視圖..我使用此代碼,但它沒有工作回到上一個ViewController在Xcode 5故事板

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; 
[back setFrame:CGRectMake(0, 0, 24, 24)]; 
[back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
[back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back]; 
self.navigationItem.hidesBackButton= YES; 
navEng.leftBarButtonItem = btnBack; 


-(void)goback{ [self.navigationController popToRootViewControllerAnimated:YES]; } 

回答

0

試試這個....只是改變最後一行代碼

UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; [back setFrame:CGRectMake(0, 0, 24, 24)]; [back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
[back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside]; 
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back]; 
self.navigationItem.hidesBackButton= YES; 
self.navigationItem.leftBarButtonItem = btnBack; 


-(void)goback 
{ 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 
0

navEng被聲明爲IBoutletUINavigationItem在頭文件和後退按鈕出現,動作正確處理,但導航到以前ViewController沒有工作

@property (nonatomic, strong) IBOutlet UINavigationItem *navEng; 
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [back setFrame:CGRectMake(0, 0, 24, 24)]; 
    [back setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
    [back addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back]; 
    self.navigationItem.hidesBackButton= YES; 
    navEng.leftBarButtonItem = btnBack; 



    -(void)goback{ 
     NSLog(@"back"); 
     [self.navigationController popViewControllerAnimated:YES]; 
    }