Swift learns _07 from scratch (learning Swift in UI interface Practice)

Source: Internet
Author: User
Tags blank page

Yesterday tried to write code after the feeling write than just a lot, so today wrote a bit of things, mainly the properties of the class, as well as inheritance and xib some knowledge, but also encountered a lot of trouble, but fortunately have been solved, here simply write the code today, there is nothing wrong place, or hope to come.

First look at the first simple page: Click the second button, the view changes color below, click the first button, modal a page

The first is some of the properties that we declared in. h before we wrote the OC code, how do we declare it in Swift? The method is as follows:

// Direct initialization of the good array, as for lazy loading method, for the time being, I still do not, still studying.    // This is used to declare the attribute, the meaning of the exclamation point here, and the personal understanding is that it needs to be an instance object    of UIButton or his subclass var button1:uibutton!     var view1:uiview!

How do we use these attributes? Please look down.

OverrideFunc Viewdidload () {//Override is the meaning of overriding the parent class methodsuper.viewdidload () Self.view.backgroundColor=Uicolor.graycolor ()//This property is not used with self. Or _ to tune, directly can write, and the previous OC in the instance variable is the same.button = Uibutton.buttonwithtype (Uibuttontype.system) asUIButton Button.frame= CGRect (x: -Y: -, Width: Max, Height: +) Button.backgroundcolor=Uicolor.whitecolor () button.settitle ("jump to next page", ForState:UIControlState.Normal) Button.addtarget (Self, Action:"PRESETNVC", ForControlEvents:UIControlEvents.TouchUpInside)//here to add a method to the button, the action is drunk, the morning has been using selector (Logtest ()), has been a problem, the afternoon so unexpectedly, the total feeling with selector is more scientific ah.self.view. Addsubview (Button) Button1= Uibutton.buttonwithtype (Uibuttontype.system) asUIButton Button1.frame= CGRect (x: -Y: $, Width: Max, Height: +) Button1.backgroundcolor=Uicolor.whitecolor () button1.settitle ("Randomly changing colors", ForState:UIControlState.Normal) Button1.addtarget (Self, Action:"ChangeColor", ForControlEvents:UIControlEvents.TouchUpInside) self.view. Addsubview (button1) Self.array. Ad Dobject ("Test") println (self.array) View1=UIView () view1.frame= CGRect (x: theY: -, Width: $, Height: $) View1.backgroundcolor=Uicolor.bluecolor () self.view. Addsubview (View1)}

As for the implementation of the BTN method, I put the code directly here.

 func ChangeColor () {//  generates a random color and changes the color of the view 
    //  Here we have to say that this random number in Swift, began to use OC's notation, has been error, Say it's UInt32, Swift is too sentimental.  View1.backgroundcolor = Uicolor (Red:cgfloat (rand ()%255 )/255.0 , Green:cgfloat (rand ()%255 )/255.0 , Blue:cgfloat (rand ()%255 )/255.0 , Alpha: 1.0  )    }
func PRESETNVC () {                /// Here the attempt to load the controller from the nib is also problematic, you need to override the parent class method, or even if you make the page, it is still a blank page         "secondviewcontroller", Bundle:nil)                truein         }    }

The following is the code in Secondviewcontroller.swift. This page I tried to use a bit of xib, but the first time, has been a blank page. I didn't understand what was going on, and finally found the reason in an article. The code comment says yes, Here's a look at the effect of the second page first.

After clicking on the BTN effect

The next step is the code section. First, here is the reason for showing the blank, and the solution

// under Swift, the subclass does not inherit the parent class's initialization method by default, so if you want to use the Init nib method, you have to add the method yourself, add, he will prompt you need to add the following method, you directly added the line, specific reasons, still in the textual research.    override init (nibname nibnameornil:string?, bundle Nibbundleornil:nsbundle?) {        Super.init (Nibname:nibnameornil, Bundle:nibbundleornil)    }    required init (coder Adecoder:nscoder) {  // Add the method        fatalerror ("init (coder:) have not been implemented" )    }

This is the property generated by the Xib connection.

    // xib file line generated, here xib usage does not change, the change is only after the wiring generated code is not the same, in fact, it is only the code is not the same, the principle is the same.         @IBOutlet var passwordtf:uitextfield!         @IBOutlet var surepasswordtf:uitextfield!

The rest is click the login button.

@IBAction func loginbtn (sender:anyobject) {//Xib How to associate file connections        if(Self.userNameTF.text.isEmpty) {//IsEmpty is a method of determining whether or not the decision is null in Swiftvar alert = Uialertview (title:"Tips", Message:"user name is empty",Delegate: Self, Cancelbuttontitle:"Determine", Otherbuttontitles:"Cancel") Alert.show ()}Else{println ("You can add some other criteria here."}}} func Alertview (Alertview:uialertview, Clickedbuttonatindex buttonindex:int) {if(Buttonindex = =0) {println ("clicked OK.")        }Else{println ("Click the Cancel")                    }    }

Conclusion.

Maybe you will feel that these are too simple, there is no technical content. What I want to say is that I have a program from only OC Ape to learn Swift process, there is no need to make things too difficult, now still learning the language of the stage, just learn how to use Swift. It's not about the difficulties of Swift.

Swift learns _07 from scratch (learning Swift in UI interface Practice)

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.