Multiple views of Xamarin.ios

Source: Internet
Author: User

Following the previous section, "Quick Start for Xamarin.ios", this section will be expanded on this basis, mainly by adding a list of records that can provide historical calls. Let's go straight to the chase.

First open the Mainstoryboard.storyboard, we continue to modify on this basis. Drag and drop a Navigation Controller into the storyboard from the toolbar, and we can see the following:

But the current main view controller is still the previous one, we need to set the new to the main view, so we'll drag and drop it to the controller below, and then we need to delete the navigation controller point that Then press CTRL and left-click Navigation Controller, drag and drop, will find a blue line, we need to say this blue line dragged to the view controller we did in the last section, and select Rootin the pop-up dropdown, This means that the root view controller of this navigation controller is the one in our last section, the result of this operation.

We can see more than one title bar on our view controller, we can double-click it and modify it to "Phoneword". Then we re-drag a button to the Call button, and the same width as the previous button, while setting the name of the button Callhistorybutton, the text displayed is call history. The interface after completion is as follows:

Now we need to complete the view controller that calls history jumps to, we first drag a table view controllers from the toolbar into the storyboard, and then open the Properties window to modify the view controller's class as " Callhistorycontroller "and enter confirm that at this time you can see that vs has generated a CallHistoryController.cs file for us, then we double-click to enter into the following code:

1 Partial classCallhistorycontroller:uitableviewcontroller2     {3          PublicList<string> Phonenumbers {Get;Set; }4 5         Private Static stringCallhistorycellid ="Callhistorycell";6 7          PublicCallhistorycontroller (INTPTR handle):Base(handle)8         {9             //registers the UITableViewCell type and the identifier for the string specified by CallhistorycallidTenTableview.registerclassforcellreuse (typeof(UITableViewCell), callhistorycellid); One  A             //specifying a data source for TableView -Tableview.datasource =NewCallhistorydatasource ( This); -Phonenumbers =Newlist<string>(); the         } -  -         Private classCallhistorydatasource:uitableviewdatasource -         { +             PrivateCallhistorycontroller Mcontroller; -  +              PublicCallhistorydatasource (Callhistorycontroller Controller) A             { atMcontroller =Controller; -             } -  -              Public OverrideUITableViewCell Getcell (UITableView tableView, Nsindexpath indexpath) -             { -                 //get table items by identifier in                 varCell =Tableview.dequeuereusablecell (callhistorycontroller.callhistorycellid); -                 introw =Indexpath.row; to                 //set the text displayed for a table item +Cell. Textlabel.text =Mcontroller.phonenumbers[row]; -                 returncell; the             } *  $              Public Overridenint rowsinsection (uitableview tableView, nint section)Panax Notoginseng             { -                 //total number of returned data the                 return  This. MController.PhoneNumbers.Count; +             } A         } the}
View Code

After completing the Callhistorycontroller , we go back to the storyboard and press CTRL and left-click to drag the Blue line from the Call History button, Put it in the Callhistorycontroller. And in the pop-up dropdown Select the first, so that when we click on the button will be implemented to jump to the specified view controller, the following we also want to modify the code in the Rootviewcontroller , in order to record the phone number when dialing, and assign the record to phonenumbers before displaying Callhistorycontroller.

We first define an array of saved phones and initialize them in the constructor:

         Public Get Set ; }          Public Rootviewcontroller (IntPtr handle)             Base (handle)        {            new list<string>();        }

Then add the following code to the beginning of the touchupinside event in Callbutton:

Phonenumbers.add (Translatednumber);

Finally, the most critical part is to pass the records past before the Callhistorycontroller is opened, so we need to rewrite the prepareforsegue method:

1          Public Override voidPrepareforsegue (uistoryboardsegue segue, NSObject sender)2         {3             Base. Prepareforsegue (segue, sender);4             varCallhistorycontroller = Segue. Destinationviewcontroller asCallhistorycontroller;5             if(Callhistorycontroller! =NULL)6             {7Callhistorycontroller.phonenumbers =phonenumbers;8             }9}

Here we access the view controller to be opened through Segue 's Destinationviewcontroller , and attempt to cast to the Callhistorycontroller type. If this is the type then the recorded data is assigned to the past, so we are done, and below we can see the final:

After clicking on Call history

Of course, in addition to directly specify the button to open the view controller, you can also code to specify the open light view controller, if we need to set the storyboard ID for these view controllers, So we need to set the storyboard ID in the Callhistorycontroller property to "Callhistorycontroller" and then comment out the code of the Prepareforsegue method, and delete the jump we set for call history in the storyboard and add the Callhistorybutton listener event in Viewdidload as follows:

1Callhistorybutton.touchupinside + = (E, s) = =2             {3Callhistorycontroller callhistory = Storyboard.instantiateviewcontroller ("Callhistorycontroller") asCallhistorycontroller;4                 if(Callhistory! =NULL)5                 {6Callhistory.phonenumbers =phonenumbers;7Navigationcontroller.pushviewcontroller (Callhistory,true);8                 }9};

Here we instantiate the view controller for the specified storyboard ID through the storyboard instantiateviewcontroller, and convert the data to the corresponding type, and if not NULL, pass it past, And this view controller is presented through the Navigationcontroller Pushviewcontoller. So far, our first two entry is over.

Multiple views of Xamarin.ios

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.