

Then finally, we simply push the view to the next controller. It does that by setting a String that we're gong to declare later. The second line is actually the core of this tutorial, because it assigns the myLabel's text to the UILabel we've placed into the SecondVC controller. The first line instantiates the SecondVC controller from the Storyboard. NavigationController?.pushViewController(myVC, animated: true) let myVC = storyboard?.instantiateViewControllerWithIdentifier("SecondVC") as! SecondVC
#Ios swift share button code#
Place the following code into the goButton() method. Now the right side will show the ViewController.swift file and you'll be ready to write some code in it. Once you're done connecting all the views, keep your XCode window split into two sections and select the first controller from the Storyboard. For the button, make sure to click the small combo box that says Outlet and switch it into Action, because we need it to be a function, not an outlet. When you release the mouse, you can give a name to the label's outlet and to the button's action in the little grey popup that shows up. swift file by holding the right mouse button (or the Control key and mouse button) over those views and dragging the blue line right below the class declaration. swift file on the right-hand side.Ĭonnect the UILabel as an IBOutlet and the UIButton as an IBAction to your. Now you'll have the Storyboard on the left and its relevant. Split the XCode window into two parts by clicking on the Assistant editor button in the top-right corner. swift file in the Class and StoryboardID fields. Select the yellow circle at the top of the controller, click on the Identify inspector panel icon on the right side of the XCode window, and type the name of your new. Now you have to link the second Controller in Storyboard with your new SecondVC.swift file.


(No spaces are allowed in the name, and it must start with a capital letter.) So, right-click in the files list panel on the left, click New File., select Cocoa Touch Class from the iOS Source category, click Next, and name the new view controller SecondVC. swift file and attach it to our second controller. On the second Controller, drag a new UILabel anywhere you want and just leave the text the way it is. I've just set its text to " Text to pass". Then double-click on the UILabel and type the text you want to pass to the second controller. You can find them in the Object library and drag them into your first controller. Now add a UILabel and a UIButton to the controller. Now select the first controller in Storyboard and click on Editor > Embed in. Start by dragging a new View Controller from the Object library into your Storyboard.Įmbed your first controller into a NavigationController, so when you push to the next controller the top bar will display a default Back button. You'll find a ViewController.swift file in the files list on the left-side panel and a controller interface in the Storyboard. Choose Single View Application and name the project however you like.

The XCode Project Setupįirst of all, create a new XCode project. We will use a String, an Integer, and also a UIImage, so keep reading-you'll be amazed at how easy this job is with Swift.
#Ios swift share button how to#
Most of the time, when you build an iOS app with more than one screen, you need to pass data between your View Controllers in order for them to share contents without losing them along the way. In this tutorial, you'll learn how to do that.
