OC iOS Development Code layout

Source: Internet
Author: User

Code layout discard Storyboard, use code to generate interface, it's pros and cons not to talk about

First, in the project settings, change the application's "portal"

Do not select Main, empty it

Then in APPDELEGATE.M, change (add content), don't forget the import

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {2     //Override point for customization after application launch.3Self.window =[[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds];4Self.window.rootViewController =[[Viewcontroller alloc]init];5 [Self.window makekeyandvisible];6Self.window.backgroundColor =[Uicolor Greencolor];7     returnYES;8}

Why???

Then you can create your own view, notice the parent class

You can then add functions in the Viewcontroller

1 -(void) loadview2{3     self.view = [[MainView alloc] Initwithframe:[uiscreen Mainscreen].bounds]; 4 }

Here Loadview is an existing function, except that you do not declare and add your own content, it will be by default, like the model in the default constructed mix, getter and setter, it is responsible for loading the interface. (By the way view is also the default

Call the method in this sub-function, initialize your view, and then add content to your view.

The following code is the contents of your own view,mainview.m and. h

First, you can rewrite

-(Instancetype) initWithFrame: (CGRect) frame

This constructor is present by default, and you can call it in Viewcontroller, even if you have not rewritten it.

You can put the UI you want to construct, and so on, in this function.

If you write directly on the rewrite, Xcode warns you not to do the parent class, it is recommended that you first construct the parent class, because now you are writing a subclass of the constructor, the thing to call is the parent class, and the parent class does not have an "instance", so the direct call, use is not correct, so first

    if (self = [Super Initwithframe:frame])    {        = [Uicolor Whitecolor];
Other Code }

You can then write your own controls in other code.

@property UILabel *lbnum;

For example, a label is defined in. h

And then we can construct it.

1 _lbnum = [[UILabel alloc]initwithframe: ({2         CGRectMake (0020  ); 3     })]; 4     [_lbnum setbackgroundcolor:[uicolor greencolor]; 5     [Self addsubview:_lbnum];

Set the parameters at random, test it.

First with the parameter construction, then set the background color so we can find it, and finally add it

Obviously it is in the upper left corner (0,0)

Then you can set it to the style you want.

The demo I wrote casually:

1-(Instancetype) initWithFrame: (CGRect) Frame2 {3     if(Self = =[Super Initwithframe:frame])4     {5Self.backgroundcolor =[Uicolor Whitecolor];6 7     }8     9     Ten      One //UILabel *lbnum; A_lbnum =[[UILabel alloc]initwithframe: ({ -CGRectMake ( -, -, Mainscreenwidth- -, myheight); -     })]; the     //[_lbnum setbackgroundcolor:[uicolor greencolor]; -[_lbnum SetText:@"There's 3 more thunder ."]; - [_lbnum Settextalignment:nstextalignmentcenter]; - [self addsubview:_lbnum]; +      -      + //Button Keys A_keys =[[Nsmutablearray alloc]init]; at     intKeycount =0; -      for(inti =0; I <3; i++) -     { -          for(intj =0; J <3; J + +) -         { -Keycount + +; in             //UIButton *key = [[UIButton alloc]initwithframe:cgrectmake ((+ mywidth) * (j+1)-mywidth, 100+myheight + i* (50+mywidt H), Mywidth, Mywidth)]; -UIButton *key =[UIButton Buttonwithtype:uibuttontypesystem]; toKey.frame = CGRectMake (( -+ mywidth) * (j+1)-Mywidth, -+myheight + i* ( -+mywidth), Mywidth, mywidth); +             //[key settitle:[nsstring stringwithformat:@ "%d (%d,%d)", Keycount,i,j] forstate:normal]; -[Key settitle:[nsstring stringWithFormat:@"? "] forstate:normal]; the [key Setbackgroundcolor:[uicolor Blackcolor]; * [_keys Addobject:key]; $ [self addsubview:key];Panax Notoginseng              -         } the     } +      A      the      +      -      $     returnSelf ; $}

Next for me to write the 9 key binding method, the method is the controller's task, so now go to Viewcontroller

Loadview in his own writing to bind, self.view, view is actually not their own mainview type, so it does not know what the subclass of their definition, so it is best to declare the type in advance, can not occupy the default view, their own definition

1 @interfaceViewcontroller ()2 3@property MainView *Mview;4 5 @end6 7 @implementationViewcontroller8 9 Ten-(void) Loadview One { A_mview =[[MainView alloc]initwithframe:[uiscreen mainscreen].bounds]; -Self.view =_mview; -      for(UIButton *onekeyinch_mview.keys) the     { - [Onekey addtarget:self Action: @selector (keyspressed:) forcontrolevents:uicontroleventtouchupinside]; -     } - } +  --(Ibaction) keyspressed: (ID) Sender + { Aprintf"1"); at}

The types and roles of several parameters Xcode auto-complement all have reminders, it is best to learn their own, the binding effect of the statement is

[Onekey addtarget:self Action: @selector (keyspressed:) forcontrolevents:uicontroleventtouchupinside];

Then you can write your own response function and complete the function you want to complete.

Attach my own little demo address

SSH [Email Protected]:kakinuma4ko/ioscodeviewdemo.git

OC iOS Development Code layout

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.