Create a Master-Detail Application based on a master-slave view
6617 단어 application
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).
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.