0

在 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

以 .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
4

1 回答 1

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>

 mynavigationItem.delegate = self;

并实现方法

-(void)shouldPushViewController{
    [self.navigationController pushViewController:viewControllerToPass animated:YES];
}
于 2012-10-10T09:21:32.450 回答