Why do IBOutlets only respond to one of the multiple view controllers? - swift

I have multiple ViewControllers in my storyboard, and I need various objects in the story board to link to vars in my code.
Screenshot of my ViewControllers
I'm only able to ctrl + drag an IBOutlet from the ViewController I initially began with, and changing the story entrance point doesn't affect the problem.
Code I am trying to link
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#objc func didTapButton(_ tap: UITapGestureRecognizer) {
}
}
extension ViewController: UIViewControllerTransitioningDelegate {
}

Make sure you specified the correct "Custom Class" for each of your view controllers.
That is, set ViewController as the custom class for each of your view controllers.
Like this:

Link the UIView class you want to connect with the UIView in the storyboard.

Related

Cannot select IBOutlet in connections

Im trying to make a simple app but am unable to make an IBOutlet like I was able to before, now you don't have the option in swift to change the connection to an Outlet instead of an action. I believe this could be because of the update in the Software as it is now Swift version 9.1. Thank you in advance!
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You have dragged to the exit on the view controller. Drag directly (ctrl) from the storyboard to the code.

Button in view doesn't work from view container

I have a view container in my view controller, which is stretched to whole screen. User interaction enabled is on. When I open another view via this view container in this view controller, button is clickable but doesn't respond. What should I do to make it work?
ViewController:
class ViewController: UIViewController {
#IBOutlet weak var viewContainer: UIView!
var views: [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
views = [UIView]()
views.append(LoginVC().view)
views.append(RegisterVC().view)
for v in views{
viewContainer.addSubview(v)
}
viewContainer.bringSubview(toFront: views[1])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func switchViewAction(_ sender: UISegmentedControl) {
print("clicked - segment")
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
and this is the button in the another view
#IBAction func registerTapped(_ sender: UIButton) {
print("Clicked - register")
}
I used Managing View Controllers tutorial to make it working.
You have to replace the entire ViewController, not just the View.
#IBAction func switchViewAction(_ sender: UISegmentedControl) {
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
Your current code is just replacing LoginVC's view with RegisterVC's view, instead of replacing LoginVC with RegisterVC. When you do that, you leave behind the ViewController in charge of actually responding to any interaction.
You'll have to rework your code to keep track of the ViewControllers instead of just their views. Then, instead of adding all of your views to viewContainer and moving them to the front when you need to change the current view, you should set the your child ViewController (the ViewController embedded in your container) to the ViewController you want to show (either LoginVC or RegisterVC).

Can't pass data between view controllers using segues (Xcode 7) (Swift)

I have been struggling to pass data between view controllers using segues. I have watched many youtube video tutorials and searched around on forums but I can't figure out why it won't work. I don't get any error messages but when I click the 'Button' the simulator crashes. I would love some help! Thanks!
Here is the code :
ViewController1 :
import UIKit
class ViewController: UIViewController {
#IBOutlet var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController : ViewController2 = segue.destinationViewController as! ViewController2
destViewController.labelText = textField.text!
}
}
ViewController2 :
import Foundation
import UIKit
class ViewController2 : UIViewController {
#IBOutlet var Label: UILabel!
var labelText : String = ""
override func viewDidLoad() {
Label.text = labelText
}
}
StoryBoard Image :
storyboard image
As stated on the comment the problem was that you were using a push segue on a UIViewController.
Please note that push and modal segues are deprecated... Please take a look at this post entry and let me know if you have any doubts
What's the difference between all the Selection Segues?
Make sure to enter some text in your label textField before tapping the button since you are passing that label's text to next scene by using force unwrap.
destViewController.labelText = textField.text! //here compiler expects that your textField has the value
I made a sample project using your same code and didn't get an error. There's nothing wrong with your code.
Try this: Go to your storyboard and check outlets inspector for each view controller and make sure you don't have an outlet to nowhere.

Opening a side menu on click on a TAB bar in SWIFT

I want to make something like below snapshots.
When I click on profile tab bar instead of opening a new view controller it shows a side menu. Is it something that has been handled on click of tabbar ?
if you want to achieve something like your screenShot then you are using a wrong library, because when you show your right viewController the front viewController go to left by amount of width of your right viewController, but anyways here is the code for what you need to do
first you need to put your viewController as delegate of your TabBarViewController and in func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool you need to return false and call the method of SWRevealViewController to show right viewController rightRevealToggleAnimated(true)
class FirstViewController: UIViewController,SWRevealViewControllerDelegate,UITabBarControllerDelegate {
#IBOutlet weak var sliderControl: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.revealViewController().delegate = self
self.tabBarController?.delegate = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//self.view.removeGestureRecognizer(self.revealViewController().panGestureRecognizer())
//self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
//checking for specific viewController
if(viewController is DesiredViewControllerClass) {
self.revealViewController().rightRevealToggleAnimated(true)
}
return false
}
}
I hope this helps you, regards
You can use the tab bar delegate:
extension ViewController: UITabBarDelegate {
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
// Present hamburger menu
}
}

Popovers in Storyboard, how to handle the delegate

In a storyboard i have a view controller which has a segue to another viewcontroller, with the "Present As Popover" applied. If i don't add any code, this works as id expect.
Issue:
I need to get the delegate working so i can get data back from the popover. I have created the delegate in the popover.
I have added
class LoginView: UIViewController,UIPopoverPresentationControllerDelegate, UIPopoverControllerDelegate,KeypadDelegate
I have added the functions to my main view controller.
Issue i have is how to set the delegate on the segue.
i have tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "KeyPadLoad"
{
let popoverViewController = segue.destinationViewController as! UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
The segue is defiantly called KeyPadLoad but when the delegate functions should be called in the popover nothing happens.
What am i doing wrong
Thanks
You have to implement the UIPopoverPresentationControllerDelegate in the class that presents the Popover. The methods of the UIPopoverPresentationControllerDelegate protocol let you customize the behavior of a popover-based presentation.
If you have set a segue to go to the Popover and you set the segue as Present as Popover then your class has to be like this :
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var popOver = segue.destinationViewController as! PopOverViewController
popOver.popoverPresentationController!.delegate = self
}
// Tells the delegate that the popover was dismissed.
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
println("dismissed")
}
}
Where the class PopOverViewController is just an UIViewController that handle the Popover, no more, something like this code :
class PopOverViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
With the above code you should see the message:
dismissed
Every time that the PopOverViewController is dismissed.
I hope this help you.