2012-10-09 64 views
0

在NavigationController中我有一個TabBarController。我有一個TabBarController的NavigationItem的自定義類,它是UINavigationItem的子類。我的NavigationItem有一個包含UIButton的TabBarButtonItem。我已經爲這個按鈕定義了一個動作。我的問題是,如何以編程方式推送到此操作的其他視圖?我怎樣才能進入這個課程的導航控制器?或者存在其他方式呢?IBAction的推視圖控制器

在.H

@interface CustomNavigationItem : UINavigationItem 
{ 
    IBOutlet UIBarButtonItem *barbtnApply; 
    IBOutlet UIButton *btnApply; 
} 
@property(nonatomic,retain) UIBarButtonItem *barbtnApply; 
@property(nonatomic,retain) UIButton *btnApply; 
-(IBAction)actionApply:(id)sender; 

@end 

中的m:

@implementation CustomNavigationItem 

@synthesize btnApply = _btnApply; 
@synthesize barbtnApply = _barbtnApply; 

-(IBAction)actionApply:(id)sender 
{ 
    btnApply = sender; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"test" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    //push to other view 
} 
@end 

回答

1

也許你應該聲明一個代理,並調用它的按鍵方法。

在CustomNavigationItem.h

@protocol CustomNavigationItemDelegate <NSObject> 

-(void)shouldPushViewController; 

@end 

@interface CustomNavigationItem : UINavigationItem{ 
     id<CustomNavigationItemDelegate> delegate; 
} 

@property (nonatomic, assign) id<CustomNavigationItemDelegate> delegate; 

在CustomNavigationItem.m

@implementation CustomNavigationItem 

@synthesize btnApply = _btnApply; 
@synthesize barbtnApply = _barbtnApply; 
@synthesize delegate; 

-(IBAction)actionApply:(id)sender 
{ 
    btnApply = sender; 
    [self.delegate shouldPushViewController]; 
} 
@end 

在viewcontroller.m

設置委託

在.H

@interface MyViewController:UIViewController <CustomNavigationItemDelegate> 

在.M

mynavigationItem.delegate = self; 

和實施方法

-(void)shouldPushViewController{ 
    [self.navigationController pushViewController:viewControllerToPass animated:YES]; 
} 
+0

感謝您這段代碼。問題是我沒有viewcontroller,我有一個tabbarcontroller,它有這個navigationitem ...這是我的故事板的截圖:http://postimage.org/image/sv6elwmcz/ –

+0

然後實現代表你的地方想推另一個viewcontroller,它只是一個例子 – btype

+0

它的作品!非常感謝你! –