Some changes in the use of swift and object-c after iOS

Source: Internet
Author: User
Tags set background

First, compare the Viewcontroller as the root of the navigation to try the controller,

Methods of Object-c

szmyviewcontroller *mvc = [[szmyviewcontrolleralloc] init];

uinavigationcontroller *nav = [[uinavigationcontrolleralloc] Initwithrootviewcontroller : MVC];

self. window . Rootviewcontroller = nav;

Swift's approach

var viewcontroller = viewcontroller();

var nav = uinavigationcontroller(rootviewcontroller:viewcontroller);

self. Window!. Rootviewcontroller = nav;

Here's a look at the UI section: a comparison between the two

1-1, the use of the original UIView

UIView *myview = [[UIViewalloc] initwithframe:cgrectmake (0.0, 100.0, 200.0, 200.0)];

MyView. backgroundcolor = [uicolor colorwithred: 155/255.0 green: 155/255.0 Blue : 155/255.0 Alpha: 1.0];

[Self. View addsubview: MyView];

1-2. Use of Swift's UIView

var myView = UIView(frame:cgrectmake(0.0, 100.0, 200.0, 200.0))

MyView. backgroundcolor = uicolor(red:155/255.0, green:155/255.0, blue:155/255.0, alpha:1.0);

Self. View. Addsubview (MyView)

2-1 use of the original Uilabel

UILabel *mylabel= [[UILabel alloc] initwithframe:cgrectmake(0.0, 100.0 , 200.0, 200.0)];

MyLabel. backgroundcolor = [uicolor colorwithred: 155/255.0 green: 155/255.0 Blue : 155/255.0 Alpha: 1.0];

MyLabel. text = @ " original UILabel";

// set Text size

MyLabel. Font = [uifontsystemfontofsize:];

// Set the color of text

MyLabel. TextColor = [uicolorredcolor];

// Set the alignment of text

MyLabel. TextAlignment = nstextalignmentcenter;

MyLabel. Linebreakmode = nslinebreakbycharwrapping;

[Self. View addsubview: MyLabel];

The use of 2-2swift's Uilabel

var myLabel = UILabel(frame:cgrectmake(0.0, 100.0, 200.0, 200.0))

MyLabel. backgroundcolor = uicolor(red:155/255.0, green:155/255.0, blue:155/255.0, alpha:1.0);

MyLabel. text = "Swift 's UILabel";

// Set the size of the text

MyLabel. font = uifont. Systemfontofsize (30);

// Set text color

MyLabel. textcolor = uicolor. Redcolor ();

// Set the alignment of text

MyLabel. TextAlignment = nstextalignment. Center;

MyLabel. Linebreakmode = nslinebreakmode. Bycharwrapping;

Self. View. Addsubview (MyLabel)

3-1 use of the original UIButton

UIButton *mybtn = [uibuttonbuttonwithtype:uibuttontypecustom];

Mybtn. tag= 110;

mybtn. BackgroundColor = [uicolorredcolor];

[mybtn addTarget:self action:@selector(mybtnselector:) forcontrolevents:uicontroleventtouchupinside];

[mybtn settitle:@ " normal "forstate:uicontrolstatenormal];

[mybtn settitle:@ " highlight "forstate: Uicontrolstatehighlighted];

[Self. View addsubview: mybtn];

-(void) Mybtnselector: (UIButton *) btn{

NSLog (@ " button's tag value is ==%i", Btn.tag);

}

The use of 3-2swift's UIButton

var mybtn = UIButton. Buttonwithtype (uibuttontype. Custom) as? UIButton;

mybtn!. frame = CGRectMake (0.0, 100.0, 200.0, 200.0);

// Set the tag value of the button

mybtn!. tag= 110;

// Set background color

mybtn!. backgroundcolor = uicolor. Greencolor ();

// set the title and status of the normal button

Mybtn?. Settitle (" normal " , forstate: uicontrolstate. Normal)

Mybtn?. Settitle (" highlight " , forstate:uicontrolstate. highlighted);

// Set button's Click event button

Mybtn?. AddTarget (Self, Action:"Mybtnselector:", forControlEvents: uicontrolevents. Touchupinside);

Self. View. Addsubview (MYBTN);

//btn button's Click event

func mybtnselector (sender:UIButton!) {

let btnindex=sender!. tag;

println (" the tag value of the button is =%i", Btnindex);

}

Related Article

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.