iOS Development Guide Chapter 7th View Controller and Navigation mode learning
Last Update:2015-09-18
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 16px;">1 overview</span></p></p><p><p><span style="font-size: 16px;">Category: Tile navigation mode label navigation mode tree navigation mode</span></p></p><p><p><span style="font-size: 16px;">2 modal view <span style="color: #ff0000;">must be a separate modal view controller</span></span></p></p><p><p><span style="font-size: 16px;">Rendering Code Method: presentViewController:animated:completion:</span></p></p><p><p><span style="font-size: 16px;">Story Board Segue Way</span></p></p><p><p><span style="font-size: 16px;">Close Dismissviewcontrolleranimated:completion:</span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Get Navigationbar: Drag a</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">editor-embed in-navigation Controller</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Create a navigation controller</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Interface Builder</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;"><span style="color: #ff0000;">Preparation</span> : Segue way to build present modally types between controllers</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Match class to the modal view controller</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;"><span style="color: #ff0000;">turn off modal view</span> : Point save to pass the parameters by broadcast PostNotificationName:object:userInfo:</span></span></p></p><p><p><span style="font-size: medium;"><span style="line-height: 24px;">Point cancle only close modal view</span></span></p></p><pre><span style="font-size: 16px;">-(ibaction) Save: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">Sender {[self Dismissviewcontrolleranimated:yes completion:</span></span>^<span style="color: #000000;"><span style="color: #000000;">{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Click the Save button to close the modal view</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); Nsdictionary</span></span>*datadict =<span style="color: #000000;"><span style="color: #000000;">[nsdictionary DictionaryWithObject:self.txtUsername.text Forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">username</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; <span style="color: #ff0000;">[[nsnotificationcenter defaultcenter]</span><span style="color: #ff0000;">postnotificationname</span>: </span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">registercompletionnotification</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil userInfo:<span style="color: #ff0000;">datadict</span>]; parameter dictionary type to pass}]; }<br></span></span></span></pre><p><p><span style="font-size: 16px;">-(ibaction) cancel: (id) Sender {</span></p></p><pre><pre><span style="font-size: 16px; line-height: 1.5;">[self<span style="color: #ff0000;"> dismissviewcontrolleranimated</span>: YES completion:^{</span></pre></pre><pre><pre><span style="font-size: 16px;">NSLog (@ "click Cancel button to close the modal view");</span></pre></pre><pre><pre></pre></pre><p><p><span style="font-size: 16px;">}];}</span></p></p><p><p></p></p><p><p><span style="font-size: 16px;">Accept the Data: also accepts AddObserver:selector:name:object by the broadcast way:</span></p></p><p><p><span style="font-size: 16px;">Get Data [notification userInfo]</span></p></p><pre><span style="font-size: 16px;">- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload{[super viewdidload]; [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (regi Stercompletion:) name:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">registercompletionnotification</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Object</span></span><span style="color: #000000;"><span style="color: #000000;">: nil];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Didreceivememorywarning{[super didreceivememorywarning]; [[nsnotificationcenter defaultcenter] <span style="color: #ff0000;">removeobserver</span>: self];}</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Registercompletion: (nsnotification*<span style="color: #000000;"><span style="color: #000000;">) Notification { <span style="color: #ff0000;">get data</span> <span style="color: #ff0000;">nsdictionary</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">*thedata =</span></span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff0000;">[notification userInfo]</span>; NSString</span></span>*username = [thedata objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">username</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">username =%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, username);}</span></span></span></pre><p><p><span style="font-size: 16px;">3 tiling navigation based on split-screen navigation</span></p></p><p><p><span style="font-size: 16px;">Preparation: Pagecontrol ScrollView The two controls do not contain relationships that are tied together and cannot be dragged Pagecontrol into the scrollView, you can use the keyboard arrow keys to adjust</span></p></p><p><p><span style="font-size: 16px;">to set the properties of a control define the method for exporting to Pagecontrol define the corresponding screen change event changepage:</span> <span style="font-size: 16px;"> </span></p></p><p><p><span style="font-size: 16px;">add picture footage: in code, you can get the view controller object in the storyboard with the storyboard ID</span> <span style="font-size: 16px;"> </span></p></p><pre><span style="font-size: 16px;">- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload{[super viewdidload]; <span style="color: #ff0000;">self.scrollview.</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">delegate =</span></span><span style="color: #000000;">self <span style="color: #000000;"> <span style="color: #ff0000;"></span>; <span style="color: #ff0000;">set content View Contentsize and frame</span> self.scrollView.contentSize</span></span>= Cgsizemake (self.view.frame.size.width*<span style="color: #800080;"><span style="color: #800080;">3</span></span><span style="color: #000000;"><span style="color: #000000;">, self.scrollView.frame.size.height); Self.scrollView.frame</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.view.frame; Uistoryboard</span></span>*mainstoryboard =<span style="color: #000000;"><span style="color: #000000;">self.storyboard; <span style="color: #ff0000;">get the Controller object from the story version</span> Uiviewcontroller</span></span>* Page1viewcontroller =<span style="color: #ff0000;"><span style="color: #ff0000;">[mainstoryboard instantiateviewcontrollerwithidentifier:@ "page1"</span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">]</span>; Self.page1</span></span>=<span style="color: #000000;"><span style="color: #000000;">page1viewcontroller.view; Self.page1.frame</span></span>= CGRectMake (<span style="color: #800080;"><span style="color: #800080;">0.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">0.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">320.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">420.0f</span></span><span style="color: #000000;"><span style="color: #000000;">); <span style="color: #ff0000;">set picture position to fit in Contentview</span> Uiviewcontroller</span></span>* Page2viewcontroller = [mainstoryboard instantiateviewcontrollerwithidentifier:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Page2</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.page2</span></span>=<span style="color: #000000;"><span style="color: #000000;">page2viewcontroller.view; Self.page2.frame</span></span>= CGRectMake (<span style="color: #800080;"><span style="color: #800080;">320.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">0.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">320.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">420.0f</span></span><span style="color: #000000;"><span style="color: #000000;">); Uiviewcontroller</span></span>* Page3viewcontroller = [mainstoryboard instantiateviewcontrollerwithidentifier:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Page3</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.page3</span></span>=<span style="color: #000000;"><span style="color: #000000;">page3viewcontroller.view; Self.page3.frame</span></span>= CGRectMake (<span style="color: #800080;"><span style="color: #800080;">2</span></span>*<span style="color: #800080;"><span style="color: #800080;">320.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">0.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">320.0f</span></span>,<span style="color: #800080;"><span style="color: #800080;">420.0f</span></span><span style="color: #000000;"><span style="color: #000000;">); <span style="color: #ff0000;">[self.scrollview addSubview:self.page1];</span> [self.scrollview addSubview:self.page2]; [self.scrollview addSubview:self.page3];}</span></span></span></pre><p><p><span style="font-size: 16px;">Implement ScrollView proxy method: call when sliding</span></p></p><p><p><span style="font-size: 16px;"><span style="color: #ff0000;">Locate the current Screen Curentpage property of Pagecontro</span></span></p></p><pre><pre><span style="font-size: 16px;">-(<span style="color: #0000ff;">void</span>) scrollviewdidscroll: (uiscrollview *<span style="color: #000000;">) ascrollview{ <span style="color: #ff0000;"> position</span> with offset</span> = <span style="color: #000000;"> ascrollview.contentoffset; </span> <span style="color: #800080;">320.0f</span> <span style="color: #000000;">;}</span></span></pre></pre><p><p><span style="font-size: 16px;">Implementing a Split-screen control event method: Point split screen control-causes scrollview offset <span style="color: #ff0000;">animation</span></span></p></p><pre><pre><span style="font-size: 16px;">-(ibaction) changepage: (<span style="color: #0000ff;">ID</span><span style="color: #000000;">) sender{ animation [UIView animatewithduration:</span><span style="color: #800080;">0.3f</span> animations:^<span style="color: #000000;">{ </span>=<span style="color: #000000;"> self.pageControl.currentPage; <span style="color: #ff0000;"></span> </span> <span style="color: #ff0000;">= Cgpointmake (320.0f * whichpage, 0.0f</span><span style="color: #000000;"><span style="color: #ff0000;">)</span>; }];}<br></span></span></pre></pre><p><p><span style="font-size: 16px; color: #000000;">4 Label Navigation tab bar for 49-point space</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Label navigation mode of each tab represents a function module, each function module is relatively independent.</span></p></p><p><p><span style="font-size: 16px; color: #000000;">The relationship Segue relationship of each viewcontrollers and Tabbarcontroller is view controllers</span></p></p><p><p><span style="font-size: 16px; color: #000000;">5 tree-shaped Structure Navigation</span></p></p><p><p><span style="font-size: 16px; color: #000000;">The hierarchical organization of information is used to construct the navigation with subordinate Relationship.</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Different levels of controller Segue Way: Show</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Check segue to set the identifier property in its property inspector, which will be used to query the Segue object in code and something for the next level of VIEW.</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Data relationships at all levels: total data is nsdict *dictdata</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Each level table view has its own listdata, and its data is derived from Dictdata</span></p></p><p><p><span style="font-size: 16px; color: #000000;">1) Root View Controller code</span></p></p><p><p><span style="font-size: 16px; color: #000000;">Called when the View jumps: prepareforsegue:sender:</span></p></p><p><p><span style="font-size: 16px; color: #000000;">The Segue.destinationviewcontroller property is used to get the view controller object to jump<br></span></p></p><p><p></p></p><pre><span style="font-size: 16px;">- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload{[super viewdidload]; <span style="color: #ff0000;">sets the proxy</span> self.tableview.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">; Self.tableView.dataSource</span></span>=<span style="color: #000000;">self <span style="color: #000000;">; <span style="color: #ff0000;">Get file Contents</span> NSBundle</span></span>*bundle =<span style="color: #000000;"><span style="color: #000000;">[nsbundle mainbundle]; NSString</span></span>*path = [bundle<span style="color: #ff0000;"><span style="color: #ff0000;">Pathforresource</span></span>:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">provinces_cities</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>OfType:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">plist</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.dictdata</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[nsdictionary alloc] initwithcontentsoffile:path]; Self.listdata</span></span>=<span style="color: #000000;"><span style="color: #000000;">[self.dictdata allKeys]; Self.title</span></span>=<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Province Information</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">pragma</span></span>mark-implementing Table View data source Method-(nsinteger) tableView: (uitableview *<span style="color: #000000;"><span style="color: #000000;">) TableView numberofrowsinsection: (nsinteger) section{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">[self.listdata count];}</span></span>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) indexpath{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>NSString *cellidentifier =<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Cellidentifier</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; UITableViewCell</span></span>*cell =<span style="color: #000000;"><span style="color: #000000;">[tableView Dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; Nsinteger Row</span></span>=<span style="color: #000000;"><span style="color: #000000;">[indexpath row]; Cell.textLabel.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[self.listdata objectatindex:row]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cell;}</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">trigger When table View Row is selected</span></span><span style="color: #000000;"><span style="color: #000000;">-(void) prepareforsegue: (uistoryboardsegue *) Segue sender: (id) sender{if ([<span style="color: #ff0000;">segue.identifier isequaltostring </span>: @ "showselectedprovince"]) {citiesviewcontroller *citiesviewcontroller = <span style="color: #ff0000;">segue.destinationviewcontroll ER</span>; Nsinteger SelectedIndex = [[self.tableview <span style="color: #ff0000;">indexpathforselectedrow</span>] row]; NSString *selectname = [self.listdata objectatindex:selectedindex]; Citiesviewcontroller.listdata = [self.dictdata objectforkey:selectname]; Citiesviewcontroller.title = selectname; }}</span></span></span></pre><p><p></p></p><p><p><span style="font-size: 16px;">2) Level two View controller code</span></p></p><pre><span style="font-size: 16px;">-(nsinteger) TableView: (uitableview *<span style="color: #000000;"><span style="color: #000000;">) TableView numberofrowsinsection: (nsinteger) section{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">[self.listdata count];}</span></span>-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *<span style="color: #000000;"><span style="color: #000000;">) indexpath{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>NSString *cellidentifier =<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Cellidentifier</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">; UITableViewCell</span></span>*cell =<span style="color: #000000;"><span style="color: #000000;">[tableView Dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; Nsinteger Row</span></span>=<span style="color: #000000;"><span style="color: #000000;">[indexpath row]; Nsdictionary</span></span>*dict =<span style="color: #000000;"><span style="color: #000000;">[self.listdata objectatindex:row]; Cell.textLabel.text</span></span>= [dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">cell;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">trigger When table View Row is selected</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) prepareforsegue: (uistoryboardsegue *) Segue Sender: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) sender{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([segue.identifier isequaltostring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">showselectedcity</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]) {detailviewcontroller</span></span>*detailviewcontroller =<span style="color: #000000;"><span style="color: #000000;">segue.destinationviewcontroller; Nsinteger SelectedIndex</span></span>=<span style="color: #000000;"><span style="color: #000000;">[[self.tableview indexpathforselectedrow] row]; Nsdictionary</span></span>*dict =<span style="color: #000000;"><span style="color: #000000;">[self.listdata objectatindex:selectedindex]; <span style="color: #ff0000;">Detailviewcontroller.url</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">= [dict objectforkey:@ "url"</span></span><span style="color: #000000;"><span style="color: #000000;"><span style="color: #ff0000;">]; </span>Detailviewcontroller.title</span></span>= [dict objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">name</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; }}</span></span></span></pre><p><p><span style="font-size: 16px;"><span style="color: #000000;"> </span>3) Level three view controller code</span></p></p><pre><span style="font-size: 16px;">- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload{[super viewdidload]; Self.webview.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Delegate</span></span>=<span style="color: #000000;">self <span style="color: #000000;">; Nsurl</span></span>* URL =<span style="color: #000000;"><span style="color: #000000;">[nsurl URLWithString:self.url]; Nsurlrequest</span></span>* Request =<span style="color: #000000;"><span style="color: #000000;">[nsurlrequest requestwithurl:url]; [self.webview loadrequest:request]; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark WebView commissioned<span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>mark---(<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) webviewdidfinishload: (uiwebview *<span style="color: #000000;"><span style="color: #000000;">) WebView {NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Finish</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) webView: (uiwebview *) webView didfailloadwitherror: (nserror *<span style="color: #000000;"><span style="color: #000000;">) Error{NSLog (</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">%@</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, [error description]);}</span></span></span></pre><p><p></p></p><p><p>iOS Development Guide Chapter 7th View Controller and Navigation mode learning</p></p></span>