-1

我想在我的 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];
}

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

代码仍然抛出异常,

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”

4

2 回答 2

1

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

我对 iOS 不太熟悉,但通常当你使用选择器时,你添加:到它@selector(addCustomInfo:)

然后函数看起来像这样:

-(void) addCustomInfo:(UIBarButtonItem *)myButton {
    NSLog(@"YOU CLICKED ME!");
}
于 2012-08-05T12:56:01.460 回答
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;
}
于 2012-08-05T14:53:26.273 回答