开发者

Adding a TTTableViewController (from three20) to another UIViewController

I'm having a problem with three20 that I'm hopeful someone will be able to help me out with.

I have a TTTableViewController that I use very similarly to how the TTTwitter app in three20's sample projects uses it. I have just three methods that I'm implementing: (id)init, (void)createModel, and (id<UITableViewDelegate>)createDelegate. And I've subclassed TTListDataSource and TTURLRequestModel for my data. To summarize, it's a fairly simple use of TTTableViewController, I'm not doing anything out of the ordinary.

When I add this TTTableViewController to a descendant of UIView, it works just fine. It loads and displays the data perfectly. For example, the following two work fine:

FooTableViewController *controller = [[FooTableViewController alloc] init];
controller.view.frame = CGRectMake(288, 20, 480, 1004);
[self.window addSubview:controller.view];

As does this:

FooTableViewController *controller = [[FooTableViewController alloc] init];
controller.view.frame = CGRectMake(288, 20, 480, 1004);

UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = CGRectMake(0, 0, 768, 1024);
// a bunch of scrollView variable initializations..
[scrollView addSubview:controller.view];
[self.window addSubview:scrollView];

The problem arises when I try to add FooTableViewController (which, again, is a subclass of TTTableViewController) to the view of a descendant of UIViewController. The following, for example, doesn't work:

FooTableViewController *controller = [[FooTableViewController alloc] init];
controller.view.frame = CGRectMake(288, 20, 480, 1004);

UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = CGRectMake(0, 20, 768, 1004);
[viewController.view addSubview:controller.view];

[self.window addSubview:viewController.view];

Any idea why this happens? Like I said, it works just fine when I add it to a UIView (that is, one that's not in a UIViewController), but doesn't when I try to add it to a UIViewController. Besides trying to add it like I did above, I also tried subclassing UIViewController and adding it from within. No luck.

Thanks!

EDIT I should be a little more clear on what I mean by "it doesn't work." All I see is an empty table -- just empty rows. Putting a breakpoint on createModel indeed shows that that method is not being called. Thanks!

UPDATE diwup totally pointed me towards the right solution. He was right in that it is because TTTableViewController's viewWillAppear: and viewDidAppear: weren't getting called. I verified this by subclassing UIViewController (I called it FooViewController) and adding the TTTableViewController to its view from within the class (I called the TTTableViewController variable controller). I also overrode 开发者_Go百科two methods like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [controller viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [controller viewDidAppear:animated];
}

And, lo and behold, it worked! Of course, this isn't a long-term solution, I still want to figure out how to get viewWillAppear: and viewDidAppear: called by itself.

UPDATE2 I decided to just override viewDidLoad: like so:

- (void)viewDidLoad {
    [super viewDidLoad];

    [super viewWillAppear:NO];
    [super viewDidAppear:NO];
}

I'm not too pleased with it, it seems hacky (and I should probably make sure viewWillAppear: and viewDidAppear: don't get called twice), but I'm ready to call it a day with it.


My answer may not be relevant. But I suspect that the problem may have something to do with the addSubview: method. Since addSubview: doesn't guarantee the view is on the top level of the view hierarchy, certain methods of that view may not get fired during or after addSubview:. These methods include viewWillAppear:, viewDidAppear: etc etc.

Therefore, if Three20's code relies on those interface methods, things could go wrong. I would suggest you try to present that controller modally and see what will happen.


The same question.

Just call the instace of TTTableViewController ;

for example:

  TTTableViewController tableViewController;
  [tableViewController createModel]

and

  [tableViewController updateView]

in viewDidLoad.

it's works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜