【问题标题】:UIViewController in UITabBar based lifecycle基于 UITabBar 的生命周期中的 UIViewController
【发布时间】:2011-06-17 18:09:40
【问题描述】:

我为我添加的每个选项卡设置了一个基于 UITabBr 的应用程序,其中包含许多 UIViewControllers (5) 现在我在为每个选项卡添加 TableView 时遇到了问题,正如我在这里指出的 UITableView in each tab 但是没有人有解决方案所以我想了想并尝试了很多方法 现在的重点是:当用户触摸选项卡时,首先在 UIViewController 中调用哪个方法? (viewDidLoad 和 viewWillAppear 不起作用)

有特定的生命周期吗?

这是正常的代码,当你点击选项卡时,控制台中没有任何显示

#

import "MusicView.h"


@implementation MusicView

@synthesize tv;



// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    //cell.textLabel.text = [elements objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSLog(@"initWithNibName works");
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"viewDidLoad works");
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

谢谢

【问题讨论】:

    标签: iphone objective-c uitableview uitabbarcontroller lifecycle


    【解决方案1】:

    您是否将自己设置为数据源和委托以及子类 UITableViewController 并订阅了各个选项卡视图控制器中的数据源和委托协议?

    【讨论】:

    • 这些是我遵循的步骤:
    • 创建了一个 UIViewController(XIB+2 类),将其与 MainWindow.xib 中的选项卡链接,然后打开刚刚创建的控制器,拖入 UITableView,将其数据源和委托链接到文件所有者,在 .m 中添加了 table view 的方法,并使用这两个委托 UITableViewDataSource、UITableViewDelegate 编辑了 .h。难道我做错了什么 ?该应用程序在这种情况下崩溃了,但在默认情况下 scode 为您提供的视图中的相同内容不会产生问题。你知道它的生命周期吗?
    • 为什么不直接在项目中添加两个 UITableViewController 子类而不是两个 UIViewController 并手动添加所有内容。从你所说的一切来看,你仍然没有在你的 .h 文件中继承 UITableViewController。
    • 我需要在同一个视图中使用更多控件,但应用程序崩溃给我这些错误 -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4b3f460 *** 由于未捕获异常而终止应用程序 'NSInvalidArgumentException ',原因:'-[UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x4b3f460' *** 首次抛出调用堆栈:
    • 因为 UIViewController 没有那个方法。你需要使用 UITableViewController...
    【解决方案2】:

    There is too much information you haven't provided. 我们不知道它来自哪个子类,也不知道委托/数据源方法。

    请按照本教程 - http://www.youtube.com/watch?v=LBnPfAtswgw

    【讨论】:

    • 我只是在项目中添加了一个 UIViewController(XIB+2 类)并将其分配给 UITabBar 的一个选项卡
    • 表格视图的一部分,你能给我关于生命周期的信息吗?关于第一个方法,称为Thanks
    • 生命周期是什么意思?
    • 调用时从类开始的所有步骤
    • 在此页面下方的评论中,我已经描述了我经历的所有步骤
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2021-08-29
    • 2011-07-30
    • 2019-09-04
    • 2016-08-06
    • 1970-01-01
    相关资源
    最近更新 更多