【问题标题】:initWithStyle not being called in UITableview from storyboard故事板的 UITableview 中未调用 initWithStyle
【发布时间】:2013-07-18 07:14:07
【问题描述】:

我正在一个项目中尝试使用这个框架VPPDropdown 但我希望它与storyboard 一起工作。

那我做了什么?我在屏幕上拖了一个UITableviewController。然后在我的代码中我有这个。

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        _dropDownSelection = [[VPPDropDown alloc] initSelectionWithTitle:@"Selection Combo"
                                                               tableView:self.tableView
                                                               indexPath:[NSIndexPath indexPathForRow:kRowDropDownSelection inSection:kSection1]
                                                                delegate:self
                                                           selectedIndex:1
                                                           elementTitles:@"Option 1", @"Option 2", @"Option 3", nil];

        _dropDownDisclosure = [[VPPDropDown alloc] initDisclosureWithTitle:@"Disclosure Combo"
                                                                 tableView:self.tableView
                                                                 indexPath:[NSIndexPath indexPathForRow:kRowDropDownDisclosure inSection:kSection1]
                                                                  delegate:self
                                                             elementTitles:@"Disclosure 1", @"Disclosure 2", @"Disclosure 3", @"Disclosure 4", @"Disclosure 5", nil];


        NSMutableArray *elts = [NSMutableArray array];
        for (int i = 1; i <= 4; i++) {
            // just some mock elements
            VPPDropDownElement *e = [[VPPDropDownElement alloc] initWithTitle:[NSString stringWithFormat:@"Element %d",i] andObject:[NSNumber numberWithInt:i]];
            [elts addObject:e];
        }

        _dropDownCustom = [[VPPDropDown alloc] initWithTitle:@"Custom Combo"
                                                        type:VPPDropDownTypeCustom
                                                   tableView:self.tableView
                                                   indexPath:[NSIndexPath indexPathForRow:kRowDropDownCustom inSection:kSection2]
                                                    elements:elts
                                                    delegate:self];
    }
    return self;
}

经过一些研究,我发现当您在 storyboard 中使用 UITableViewController 时。上面的方法没有被调用。你应该把那个代码放在里面:

-(id)initWithCoder:(NSCoder *)aDecoder {
    if ( !(self = [super initWithCoder:aDecoder]) ) return nil;

    // Your code goes here!

    return self;
}

当我这样做时,我得到以下错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle &lt;/Users/stefgeelen/Library/Application Support/iPhone Simulator/6.1/Applications/6D0EF4BF-068C-493C-A1A5-CFAA2B935D56/claesdistribution.app&gt; (loaded)' with name 'DR7-d0-mee-view-dDq-is-22f''

谁能帮我解决这个问题?

【问题讨论】:

  • 如果你不执行initWithCoder,一切都会正常工作。

标签: iphone ios objective-c uitableview uistoryboard


【解决方案1】:

当使用 Interface Builder 时,你应该把初始化代码放在这个方法中:

- (void)awakFromNib
{

}

注意:虽然没有 [super awakFromNib] 可以调用,也不要调用 [super initWithStyle]。

还应注意,控制器中的许多 UI 元素可能尚未在 awakFromNib 中初始化,因此应在 viewDidLoad 中更好地访问其中一些元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2023-04-06
    • 2016-10-16
    相关资源
    最近更新 更多