代码之家  ›  专栏  ›  技术社区  ›  Maurice

UISplitView在DetailView中具有多个ViewController(情节提要)

  •  0
  • Maurice  · 技术社区  · 12 年前

    我正在寻找一个解决方案,在DetailView(UISplitView的右视图)中具有多个ViewController。

    苹果的例子很好,但使用了笔尖文件而不是故事板。( https://developer.apple.com/library/ios/samplecode/multipledetailviews/Listings/ReadMe_txt.html )

    我找到了另一个示例,但在实现UITableView时存在空白 http://www.dharmaworks.net/Consulting/switching-detail-views-in-uisplitviewcontroller-with-ios7

    1 回复  |  直到 12 年前
        1
  •  0
  •   Maurice    12 年前

    在寻找答案的过程中,我发现许多人都有同样的问题。我自己想好了,所以这里是我的解决方案。我希望它对其他人有用。

    步骤1。 创建SplitViewController项目。如果您已经有项目,请跳过此步骤;) enter image description here

    步骤2。 添加两个不同的视图控制器。在本例中,我称它们为AbcViewController和XyzViewController。 enter image description here enter image description here

    步骤3。 转到ipad情节提要,从情节提要中删除DetailViewController。然后添加两个新的viewController。

    enter image description here

    步骤4。 设置viewControllers的类和情节提要ID。 enter image description here

    步骤5。 转到MasterViewController.h并用以下代码替换代码。

    #import <UIKit/UIKit.h>
    
    @class AbcViewController;
    @class XyzViewController;
    
    @interface MasterViewController : UITableViewController
    
    @property (strong, nonatomic) AbcViewController *abcViewController;
    @property (strong, nonatomic) XyzViewController *xyzViewController;
    
    @end
    

    步骤6。 现在转到MasterViewController.m文件并替换为以下代码:

    注: 如果您有一个现有项目并且不想替换,请使用步骤7中的代码。

    #import "MasterViewController.h"
    #import "DetailViewController.h"
    
    @interface MasterViewController () {
        NSMutableArray *_objects;
    }
    @end
    
    @implementation MasterViewController
    
    - (void)awakeFromNib
    {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.clearsSelectionOnViewWillAppear = NO;
            self.preferredContentSize = CGSizeMake(320.0, 600.0);
        }
        [super awakeFromNib];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.detailViewController = (DetailViewController*)[[self.splitViewController.viewControllers lastObject] topViewController];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Table View
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 2;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
        if (indexPath.row == 0) {
            cell.textLabel.text = @"ABC";
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"XYZ";
        }
    
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.abcViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"];
        self.xyzViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"XYZ"];
        if (indexPath.row == 0) {
            NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.abcViewController, nil];
            self.splitViewController.viewControllers = newVCs;
        }
        if (indexPath.row == 1) {
            NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.xyzViewController, nil];
            self.splitViewController.viewControllers = newVCs;
        }
    }
    
    @end
    

    步骤7。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            self.abcViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ABC"];
            self.xyzViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"XYZ"];
    
            if (indexPath.row == 0) {
                NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.abcViewController, nil];
                self.splitViewController.viewControllers = newVCs;
            }
            if (indexPath.row == 1) {
                NSArray *newVCs = [NSArray arrayWithObjects:[[[self splitViewController ] viewControllers ] firstObject ] , self.xyzViewController, nil];
                self.splitViewController.viewControllers = newVCs;
            }
        }
    

    就这样,运行您的项目并享受:)