Create a Master-Detail Application based on a master-slave view

6617 단어 application
Apps created with the Master-Detail Application work on both iPad and iPhone, and while the template solves most of the issues, there are some Apple legacy issues that need to be addressed. When creating a project using this template, you need to set the drop-down list Device Family to Universal, and do not select the checkbox Use Core Data.
The project contains two storyboards by default, one for the iPhone (MainStoryboard_iPhone.storyboard) and one for the iPad (MainStoryboard_iPad.storyboard).
Open MainStoryboard_iPad_storyboard and you can see a split view controller connected to two navigation controllers (UINavigationController). The main navigation controller is connected to a scene containing a table view (UITableView), which is the main scene, and is handled by the MasterViewController class; the detail navigation controller is connected to a simple empty scene (UIViewController), which is handled by the DetailViewController class.
MainStoryboard_iPhone.storyboard is much simpler, a navigation controller (UINavigationController) is connected to two scenes. The first is the master scene (MasterViewController) and the second is the detail scene (DetailViewController).
One detail to pay attention to is: under the iPhone storyboard, if you modify the UITableView in the main scene and change the Content from the default value of Static Cells to Dynamic Prototypes, the connection between the main scene and the detail scene will be lost after the modification. To fix the connection, Control-drag from a cell (not a table) to the Details scene and select Push when prompted by Xcode.
Whether it's an iPhone or iPad storyboard, the basic process is the same: step 1: The main scene contains a UITableView and loads the data. step 2: Click on a Cell, pass the data object contained in the Cell to the detailed information scene, and jump or load the detailed information scene. Step 3: After the detailed information scene is loaded, perform the corresponding initialization work according to the received Cell data object.
For step 1, it is mainly the main method to implement the data source protocol (UITableViewDataSource) requirements of the table view. This step is completely consistent with the previous method of implementing the table view separately.
Step2 mainly deals with the tableView:didSelectRowAtIndexPath: method of the delegate protocol (UITableViewDelegate) of the table view. When the data object of the Cell is collected, the object is passed to the detailed information scene. Possible codes are as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailViewController.detailItem = [[flowerData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
Step3 is to obtain the detailItem set by step2 through a method named configureView after the detailed information scene is loaded, and do corresponding processing. Possible codes are as follows:

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) 
    {
        NSDictionary *dict = self.detailItem;
        
        self.navigationItem.title = [dict objectForKey:@"name"];
        
        NSURL *url = [NSURL URLWithString:[dict objectForKey:@"url"]];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.detailWebView loadRequest:request];
        
        self.detailDescriptionLabel.hidden = YES;
    }
}
Generally, after the above three steps are completed, the application can run normally on the iPad; but the iPhone version has a small problem, that is, when the user clicks on the Cell, the object of the detailed information scene cannot be directly obtained. You can use the following code to verify :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.detailViewController)
    {
        NSLog(@"i got it");
    }
    else
    {
        NSLog(@"bad luck,it's nil");
    }
}
Under iPhone, self.detailViewController gets a nil, but under iPad, the object can be obtained correctly. This is because iPad is managed by split view controller and can easily access the view controller of another scene. To fix this problem, you need to set the value of self.detailViewController in the prepareForSegue:sender: method first:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    self.detailViewController = segue.destinationViewController;
}
When this is set up, the app works fine on both the iPad and iPhone. In general, it is relatively simple to create a project based on the Master-Detail Application. You only need to pay attention to the two problems that the iPhone version will encounter (1. The connection is lost due to changing the Content of the table view; 2. Self. detailViewController is nil).

좋은 웹페이지 즐겨찾기