0

NavigationController には、TabBarController があります。UINavigationItem のサブクラスである TabBarController の NavigationItem のカスタム クラスがあります。私の NavigationItem には、UIButton を含む TabBarButtonItem があります。このボタンのアクションを定義しました。私の質問は、このアクションから他のビューにプログラムでプッシュするにはどうすればよいですか? このクラスでナビゲーションコントローラーを取得するにはどうすればよいですか? または、これには別の方法がありますか?

.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 に答える