TUTORIAL iOS! How to change label text on button click in iOS App

in #utopian-io8 years ago (edited)

recording.gif
Hello to all, this is @iamankit. I am iOS Developer for past 3 years. I have strong knowledge in objective c and swift language. During my 3 years carrier i worked on many iOS Projects and gain good knowledge in iOS Development. Today i am going to share my knowledge with you in form of a tutorial.
So in today's tutorial i am going to show you how to change label text when user clicks on button.
I am assuming that readers have basic knowledge of using iOS Programming and Xcode like creating app in Xcode and designing views on Storyboards. If you want to learn iOS from scratch then i going to start a series soon of that program too. So let's start.

First of all open your Xcode and create a new project by clicking on create new Xcode project.
Screen Shot 2018-01-11 at 12.09.07 PM.png
After this we got a popup asking of what type of app we want to make with some options, just select Single View App and click next. After clicking next we come to new screen where we have to enter details of our app like product name and other things. For now just fill product name and if you want to change your developer certificate then change it in Team column and click next. Now last step Xcode ask is where we want to save our project after creation.
Screen Shot 2018-01-11 at 12.16.44 PM.png
So our project is created and now we have to make screen in your app. To make screen go to Main.Storyboard file in your left side panel. Here you will get a view controller with white background. Actually this view controller is the screen we will see when we run our app in simulator. If we run the app now in simulator we get blank white screen in our with nothing on it. So first we need to add label and a button on the screen. First of all let's change background colour of view controller from attribute inspector to whatever you want, then go to bottom of your right hand side section of Xcode there you find a filter search bar type label in the bar and drag label from there to your view. Do same to add button to your view, search for button in bar and drag button to view too. Now change button width and height and label width, height and text (write something instead of label) also add constraints to position label and button on view. Change Button name. Now your view will look something like this -
Screen Shot 2018-01-11 at 1.02.47 PM.png

Now we have to make outlets of label and button in our view controller file. Xcode auto creates a file with name of viewcontroller.swift. If we want to do some programmatically on our view then we need to write code in our viewcontroller.swift file. So make outlets of lable and button in your viewcontroller.swift file from storyboard. As we want to change label on button click then we need to make action outlet for button in our viewcontroller.swift file. After connecting outlets your viewcontroller.swift will looks like this.
Screen Shot 2018-01-11 at 1.09.27 PM.png

Ok half of our work is done. Now we need to write code to change text of label on button click. So go to viewcontroller.swift file and in buttonclick function write this code -

self.labelOutlet.text = "Thanks for liking utopian-io @iamnakit. Have a great journey ahead!"

Screen Shot 2018-01-11 at 1.17.35 PM.png

Ok all done now run your app in simulator and click on button. Whoila! text of label is changed. That's it today we learn how to change label text on button click.
Stay tuned for more tutorials and also for my iOS 11 Expert from Scratch Program. Have a nice day friends.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for your contribution,
it can not be approved because there are numerous grammatical and spelling errors and it was posted under the wrong category.


Please try to run your future posts through a grammar/spelling checker tool like grammark.org and/or Grammarly in the future and also try to post in the most fitting category for the content. See the Utopian Rules.


You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.034
BTC 114681.82
ETH 4519.56
SBD 0.85