The combination of the ring-letter SDK and Apple Watch (3)

Source: Internet
Author: User

The 3rd chapter focuses on how to display the data in the iphone program on the Watch app's page. The main operation is "Emwatchocdemo WatchKit Extension" this folder.

If you've seen the blog I recommended in the 1th chapter, you should understand that this target is primarily responsible for logic, getting data from the iphone app, and mobilizing the Watch app to display the data.

The default is this way.

Wathkit defines some classes specifically for watch apps, as compared to the Uikit

Second, integrated watch app and iphone app

1. New Controller

According to Interface.storyboard, I need to create a new 5 controller. Right-click---New File---Cocoa Touch Class

The new class defaults to three methods, [-awakewithcontext:] equivalent to [-viewdidload],[-willactivate] equivalent to [-viewwillappear ],[-diddeactivate] equivalent to [-viewdiddisappear], "equivalent" Is it easy to understand what can be done in each method?

Once the 5 controllers have been built, open Interface.storyboard again and fill in the corresponding class name in the Class attribute of each storyboard controller, for developers who are familiar with storyboard Should be no stranger.

A picture of the drawings

2. After associating a custom class with storyboard, continue to associate the other controls.

Declares a plug-in variable table and associates it in storyboard.

@property (Weak, nonatomic) Iboutlet wkinterfacetable *table;

Create a custom table Row Controller, right----the New File---Cocoa Touch Class---Subclass of "nsobject", declare the plug-in variable label, and in storyboard the table The Row controller is associated with the label. Remember to fill in the identifier of the table Row controller, which is used when loading data.

3. The next step is to get the data for each page, and I'm getting the data in [-Awakewithcontext:] .

Wkinterfacecontroller has a class method [+ openparentapplication: reply:], used to initiate an application to the corresponding iphone app.

and the corresponding iphone app wants to detect this request, need to listen in Appdelegate [- application: handlewatchkitextensionrequest: reply:].

Take the menu page Menucontroller as an example, when the page is loaded to the iphone app to start to get a login request, the iphone app received a request to return the value of the login to WatchKit Extension, if not signed in, the page displays "login" option, if logged in, displays the three options for "session" Friends "group".

Menucontroller:

[Wkinterfacecontroller openparentapplication:@{@"Action":@"islogined"} reply:^ (Nsdictionary *replyinfo, Nserror *error) {BOOL islogined=NO; if([Replyinfo Count] >0) {islogined= [[Replyinfo objectforkey:@"islogined"] boolvalue]; }           if(islogined) {nsdictionary*conversationinfo = [Nsdictionary Dictionarywithobjectsandkeys:@"Session",@"title", nil]; Nsdictionary*friendinfo = [Nsdictionary Dictionarywithobjectsandkeys:@"Friends",@"title", nil]; Nsdictionary*groupinfo = [Nsdictionary Dictionarywithobjectsandkeys:@"Group",@"title", nil];             [Self.datasoure Addobject:conversationinfo];             [Self.datasoure Addobject:friendinfo];                      [Self.datasoure Addobject:groupinfo]; Nsinteger Count=[self.datasoure Count];//@ "Rowtype2controller" is the identifier property of the table Row Controller mentioned above[Self.table setnumberofrows:[self.datasoure Count] Withrowtype:@"Rowtype2controller"];  for(inti =0; I < count; i++) {Rowtype2controller*rowcontroller =[self.table rowcontrolleratindex:i]; Nsdictionary*dic =Self.datasoure[i]; NSString*title = dic[@"title"];             [Rowcontroller.titlelabel Settext:title]; }        }        Else{//@ "Rowtype2controller" is the identifier property of the table Row Controller mentioned above[Self.table setnumberofrows:1Withrowtype:@"Rowtype2controller"]; Rowtype2controller*rowcontroller = [self.table rowcontrolleratindex:0]; [Rowcontroller.titlelabel SetText:@"Login"]; }  }]; 

Appdelegate

- (void) Application: (UIApplication *) application handlewatchkitextensionrequest: (nsdictionary *) UserInfo reply: (void(^) (Nsdictionary *)) reply{if([UserInfo Count] >0) {NSString*actionstring = [UserInfo objectforkey:@"Action"]; Easemob*easemob =[Easemob sharedinstance]; if([actionstring isequaltostring:@"islogined"]) {reply (@{@"islogined": [NSNumber numberwithbool:[easemob.chatmanager isLoggedIn]}); }}

4. Get the data, then call the Watch app to display the data.

the display data is mainly used in the wkinterfacetable . wkinterfacetable compared to UITableView, the number of interfaces that can be called is poor

Wk_class_available_ios (8_2)@interfaceWkinterfacetable:wkinterfaceobject- (void) Setrowtypes: (Nsarray *) rowtypes;//row names. Size of array is number of rows- (void) Setnumberofrows: (Nsinteger) numberofrows withrowtype: (NSString *) RowType;//repeating row name@property (nonatomic,ReadOnly) Nsinteger numberofrows;- (ID) Rowcontrolleratindex: (nsinteger) index;- (void) Insertrowsatindexes: (Nsindexset *) rows Withrowtype: (NSString *) RowType;- (void) Removerowsatindexes: (Nsindexset *) rows;- (void) Scrolltorowatindex: (nsinteger) index;@end
WKInterfaceTable.h
-(void) Table: (wkinterfacetable *) Table Didselectrowatindex: (Nsinteger) RowIndex;  // row Selection If controller has wkinterfacetableProperty-(ID) Contextforseguewithidentifier: ( NSString *) Segueidentifier intable: (wkinterfacetable *) Table RowIndex: (Nsinteger) RowIndex; -(Nsarray *) Contextsforseguewithidentifier: (NSString *) segueidentifier intable: (wkinterfacetable *) Table RowIndex: ( Nsinteger) RowIndex;
in Wkinterfacecontroller

The code example in the previous step has given the wkinterfacetable how to use it, see Demo for specific code.

5, each individual page has been written, now want to let them move up.

WatchKit provides three types of page navigation.

The first kind UINavigationController of control of a similar stack of navigation, the corresponding interface

-(void) Pushcontrollerwithname: (NSString *) name context: (ID) context;  // context passed to child controller via Initwithcontext:-(void) Popcontroller; -(void) Poptorootcontroller;

The second form of modal, the corresponding interface

-(void) Presentcontrollerwithname: (NSString *) name context: (ID//  modal Presentation-(void) Dismisscontroller;

The third Kind UIPageController of page-like navigation, the corresponding interface

-(void//  modal presentation of paged controllers. Contexts matched to Controllers-(void< /c3>) Becomecurrentpage;

The "Withname (s):" parameter is the identifier property that each control sets in storyboard.

Well, write so much first, there will be time to continue to add later.

The combination of the ring-letter SDK and Apple Watch (3)

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.