如何在NavigationController上访问链接到UIBarButton的方法

时间:2012-08-05 12:37:39

标签: iphone objective-c ios uinavigationcontroller uikit

我想在我的UINavigationController中添加一个UIBarButton。我在以下代码的帮助下做到了这一点,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:@"Add Info" style:UIBarButtonItemStylePlain target:self action:@selector(addCustomerInfo)];
    self.navigationItem.rightBarButtonItem = addInfoButton;
}

-(void) addCustomerInfo
{
    AddInfoViewController *addVC = [[AddInfoViewController alloc] initWithNibName:@"AddInfoViewController" bundle:nil];

    [addVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];

    [self presentModalViewController:addVC animated:YES];
}

我应该在.h文件中声明“-(void) addCustomerInfo”吗?我已经尝试过,但没有运气。

代码仍会抛出异常,

2012-08-06 04:16:22.200 TableView [5698:f803] - [RootViewController addCustomerInfo]:无法识别的选择器发送到实例0x6c662b0 2012-08-06 04:16:22.202 TableView [5698:f803] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [RootViewController addCustomerInfo]:无法识别的选择器发送到实例0x6c662b0'

2 个答案:

答案 0 :(得分:1)

查看本教程: http://www.innovatelabs.in/2010/03/implementing-uibarbuttonitem/

我对iOS不太熟悉,但通常在使用选择器时,您向其添加: @selector(addCustomInfo:)

然后函数看起来像这样:

-(void) addCustomInfo:(UIBarButtonItem *)myButton {
    NSLog(@"YOU CLICKED ME!");
}

答案 1 :(得分:1)

您班级的代码是正确的。您需要更改您的App Delegate

我建议您在App Delegate中创建一个属性来存储导航控制器 - @property (strong, nonatomic) UINavigationController *navController; - 不要忘记合成它。 然后,在创建导航控制器时,将其设置为您的属性 - self.navController = [[UINavigationController alloc] init];

这将保证您的NavigationController被正确保留,并且您的应用程序中的其他类可以正确访问它。

下面的一些示例代码可能会更清晰:

首先是AppDelegate头文件:

//AppDelegate.h
#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *navController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;    
@end

执行文件:

//  AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.navController = [[UINavigationController alloc] init];    
    UIViewController *myVC= [[myVC alloc]     init];
    [self.navController pushViewController:myVC animated:NO];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}