簡體   English   中英

如何從UIView子類調用UIViewController?

[英]How to call a UIViewController from a UIView SubClass?

我有主UIViewController ,其中有一個UITableView

UItableView的單元格中,我已經添加了UICollectionView

在-

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 

我正在調用主UIViewController的方法。

UIViewController *view = [[UIViewController alloc] init];
[view my_method];

在主要觀點上。

-(void)my_method {
    AnotherName *view_my=[[AnotherName alloc]initWithNibName:@"AnotherName" bundle:nil];
        [self.navigationController pushViewController:view_my animated:NO];
}

Viewdidload方法正在調用,但view未顯示。

這是因為您通過調用[[UIViewController alloc] init] 這總是返回空視圖。

你應該叫這個

  • 如果您使用情節提要,請為視圖控制器提供一個情節提要ID,並將其命名為

MainViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"storyboard id"];

  • 如果您使用界面構建器,請調用此

MainViewController *controller = [[MainViewController alloc] initWithNibName:@"Nib File Name" bundle:nil];

我有解決辦法..

AnotherName *view_my = [[AnotherName alloc]initWithNibName:@"AnotherName" bundle:nil];
self.window.rootViewController = view_my;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

特別感謝@The Mech System ..為了幫助我找到解決方案..現在它可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM