【问题标题】:iOS: Returning to main XIB from secondary XIBiOS:从辅助 XIB 返回主 XIB
【发布时间】:2012-08-10 00:36:57
【问题描述】:

在使用第二个 XIB 后,我正在尝试将其更改回主 XIB。这是我正在使用的当前代码:

NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"Results1" owner:self options:nil];
    UIView *aView = [nibObjs objectAtIndex:0];
    self.view = aView;

我在第二个 xib 上有一个返回原始 xib 的按钮。当我按下按钮时,应用程序崩溃了。我不知道为什么,因为它没有显示错误 - 它只是崩溃。这是第二个 xib 或“Results1” xib 文件上按钮上的代码:

-(IBAction)Button100:(id)sender
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"main" owner:self options:nil];
UIView *aView = [nibObjs objectAtIndex:0];
self.view = aView;
}

不知道我做错了什么。

【问题讨论】:

    标签: objective-c ios xib


    【解决方案1】:

    改为添加为子视图,这样您就可以“弹出”Results1 视图了:

    // in your .h
    @property (strong) UIView *aView;
    
    // replace your provided code with this
    NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"Results1" owner:self options:nil];
    self.aView = [nibObjs objectAtIndex:0];
    [self.view addSubview:self.aView];
    
    - (IBAction)Button100:(id)sender
    {
        [self.aView removeFromSuperview];
        self.aView = nil;
    }
    

    只是一个建议——如果您希望它们“交换”,请考虑在 UIView 中使用类方法 transitionFromView:toView:duration:options:completion:

    【讨论】:

    • 它似乎不起作用,我想我没有解释抱歉,第一组代码在一个 xib 中,第二组代码在另一个 xib 文件中,当我替换代码时它似乎没有上班
    • 代码的第二部分不起作用我不知道为什么? [self.aView1 removeFromSuperview]; self.aView1 = nil;
    • 这就是我想出的
    【解决方案2】:

    问题是我需要关闭自动引用计数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 2015-10-27
      • 2011-07-13
      • 2013-01-24
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多