1

当我运行以下代码时,它给出了上述错误

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    [self.window makeKeyAndVisible];

    self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.firstViewController=[[FirstViewController alloc]initWithNibName:nil bundle:nil];
    UINavigationController *localNavigationController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.navigationController=localNavigationController;
    [localNavigationController release];
    UINavigationController *localFistNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
    self.firstNavigationController=localFistNavigationController;
    [localNavigationController release];
   NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];
    UITabBarController *localTAbBarController =[[UITabBarController alloc]init];
    [localTAbBarController setViewControllers:twoBars];
    self.tabBarController=localTAbBarController;
    [localTAbBarController release];
    [self.window addSubview:self.tabBarController.view];

        return YES;
}

如果我运行以下代码,它运行良好

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
         [self.window makeKeyAndVisible];

    self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];

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

    self.firstNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
    NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];

    self.tabBarController=[[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:twoBars];

       [self.window addSubview:self.tabBarController.view];



    return YES;

我不明白这些之间有什么区别。在第一个中,我刚刚创建了局部变量并将其分配给属性。在后面的一个中直接使用了属性。为什么它给出错误程序接收信号“EXC_BAD_ACCESS”

4

4 回答 4

2

我认为在第一个中您发布了一些代码,然后在再次发布您之后再次发布该对象,例如:

[localTAbBarController release];这个。所以可能这就是你收到错误程序的原因

信号“EXC_BAD_ACCESS”。在第二个中你很好地使用你的对象没有发布所以它的工作

美好的。

于 2012-12-28T07:25:25.900 回答
1

更新:

嘿,我使用你的代码,在这里你得到 BAD_ACCESS 在下面一行看到..

[localNavigationController release];

只是评论它,你没有 BAD_ACCESS

于 2012-12-28T07:37:29.303 回答
0

只需检查这一行。

self.firstNavigationController=localFistNavigationController;
  -->>  [localNavigationController release];  

它应该是[localFistNavigationController release];

于 2012-12-28T15:17:25.933 回答
0

我得到了答案。由于多次释放相同的对象,它会发生。我已经发布[localNavigationController release];了两次。以后一定是

[localFirstNavigationController release];
于 2012-12-28T08:47:46.873 回答