【问题标题】:How to load entirely different ViewController in a Master-Detail app?如何在 Master-Detail 应用程序中加载完全不同的 ViewController?
【发布时间】:2012-05-14 07:22:34
【问题描述】:

我有一个 Master-Detail 应用程序,我想通过单击 Master 中的行来加载一个新视图。如果我选择第 1 行,则视图不同,并且在选择不同的行时,新视图将显示在详细信息部分。我怎样才能做到这一点?

我正在做这样的事情。问题是 - 如果我第一次选择这 2 行中的任何一行,视图实际上会发生变化。但是当我再次点击另一行时,它并没有改变。

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

if(indexPath.row==0){


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterTweetViewController *detailView=(TwitterTweetViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}


if(indexPath.row == 1)
{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterDetailViewController *detailView=(TwitterDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}

}

我应该怎么做才能改变选择这些行的视图?

【问题讨论】:

标签: ios xcode ipad ios5


【解决方案1】:

有一个很棒的方法:

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

使用它。在单击单元格后立即调用它。你可以在那里做一些检查。简短的例子:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.row == 0) {
    SomeViewController *svc = [[SomeViewController alloc]initWithNibName:@"SomeViewController" bundle:nil];
    [self.navigationController pushViewController:svc];
  } else if (indexPath.row > 0 && indexPath.row < 5) {
    SomeOtherViewController *sovc = [[SomeOtherViewController alloc]initWithNibName:@"SomeOtherViewController" bundle:nil];
    [self.navigationController pushViewController:sovc];
  } else {
    AnotherViewController *avc = [[AnotherViewController alloc]initWithNibName:@"AnotherViewController" bundle:nil];
    [self.navigationController pushViewController:avc];
  }
  //some other code if needed
}

希望对你有帮助

【讨论】:

  • 出现此错误 - 2012-05-14 13:56:45.232 SpltiView With Views[26907:f803] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法加载 NIB在捆绑包中:'NSBundle /nihal/Library/Application Support/iPhone Simulator/5.1/Applications/9D894362-800E-4BE1-BE2B-2F7E2C3DB8A3/SpltiView With Views.app>(已加载)',名称为 'NewViewController'' * ** 第一次抛出调用栈:终止调用抛出异常
  • 因为主细节已经为默认细节视图控制器定义了一个导航控制器 - 你认为使用 [self.navigationController pushViewController:svc] 会工作吗?
  • @NihalSharma 抱歉,没看到它是为 iPad 设计的
【解决方案2】:

您可以将视图控制器的枚举创建为:-

enum MyViewControllers
{
viewcontroller1
viewcontroller2
viewcontroller3
}

枚举中的常量将对应于每个 indexPAth 的视图控制器。

然后在 UITableVIew didSelectRowAtIndexPath: 的委托方法中,使用 switch case :-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
    {
    switch(indexPath.row)
{
    case 1:
          Load view1
          break;
    case 2:
          Load view2
          break;
    case 3:
          Load view3
          break;     .
         .
         so on.
    }
}

或者在不同的 NavigationControllers(如 iPad 中)的情况下,您将必须使用委托和协议传递数据。在 rootViewController 中定义协议,然后在细节中设置其委托(您想要推送视图控制器的位置)

类似的东西:-

@protocol SendSelectedINdexDelegate
{
@required
-(void)sendTheIndex:(NSNumber)number.
@end

在界面中

id <SendSelectedINdexDelegate> delegateSend

设置它的属性:-

@property (nonatomic) id delegateSend;

在.m

@synthesize delegateSend

在 didSelectRowAtIndexPath:-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[self delegate]sendTheIndex:indexPath.row];

}

在接收控制器 符合协议 并设置

rootview.delegateSend=self;

并实现协议的方法;

-(void)sendTheIndex:(NSNumber)number
{
//nsnumber is your index perform operation based on this.
}

【讨论】:

  • 我认为它不会起作用。请提出其他建议。
  • [自我委托]sendTheIndex:indexPath.row];应为:[[self delegateSend]sendTheIndex:indexPath.row];但是你会有一个不兼容的整数到指针转换,将 NSInteger 发送到 NSNumber 参数。
【解决方案3】:

请注意,如果您使用 ARC,它会禁止合成具有未指定所有权属性的属性(在 .m @synthesize delegateSend 中)。

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 2013-03-14
    • 2013-01-07
    • 1970-01-01
    • 2015-11-11
    • 2012-03-24
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多