从IBAction推送View Controller

时间:2012-10-09 07:59:53

标签: objective-c uinavigationitem pushviewcontroller

在NavigationController中,我有一个TabBarController。我有一个TabBarController的NavigationItem的自定义类,它是UINavigationItem的子类。我的NavigationItem有一个TabBarButtonItem,它包含一个UIButton。我已为此按钮定义了一个动作。我的问题是如何以编程方式从此操作推送到其他视图?我怎样才能进入这个类的导航控制器?或者存在另一种方式吗?

在.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

in .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 个答案:

答案 0 :(得分: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

设置代理

in .h

@interface MyViewController:UIViewController <CustomNavigationItemDelegate>

in .m

 mynavigationItem.delegate = self;

并实施方法

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