我在 Xcode 4.3 中创建了一个新项目“空应用程序”模板,它只有两个类AppDelegate.h
& .m
我在创建应用程序时检查了 ARC 以使用自动引用计数。
我添加了两个新文件“RootViewController”和“NewProjectViewControllers”。
我实现了代码来设置导航控制器,如下所示AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
rootViewController = [[MainViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigation.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
并在 hte 主页视图(根视图控制器)中实现如下
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Projects";
UINavigationBar *navigationBar = [self.navigationController navigationBar];
[navigationBar setTintColor: [UIColor colorWithRed:10/255.0f green:21/255.0f blue:51/255.0f alpha:1.0f]];
//To set the customised bar item
UIButton *rightBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBarBtn setBackgroundImage:[UIImage imageNamed:@"plus_new.png"] forState:UIControlStateNormal];
rightBarBtn.frame=CGRectMake(0.0, 100.0, 30.0, 30.0);
[rightBarBtn addTarget:self action:@selector(addProject) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
self.navigationItem.rightBarButtonItem = rightBarItem;
// Do any additional setup after loading the view from its nib.
}
- (void) addProject
{
NewProjViewController *editProject = [[NewProjViewController alloc] init];
[self.navigationController pushViewController:editProject animated:YES];
NSLog(@"xxxxxxxxxxxxxxx");
}
但是由于我使用ARC
导航可能会立即解除分配并且它不起作用,因此方法中的所有操作都有效,除了推送到下一个视图
如果我在没有 ARC 的情况下做同样的事情,它工作正常
如何解决这个问题..?提前致谢