Search code examples
iosswifttableviewibaction

Swift addsubview and app crash when IBAction occur


I am just adding subview from other view controller to my current view controller (which consist of tableview). I add subview when user press row.

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{
    var vc: alertVC = alertVC()
    vc.setUpData(Helper.Stored.alertViewWithPlacehlder)
    self.view.addSubview(vc.view)
}

After that, in that alertVC, there is IBAction. When I press that button, my app crush. I also don't know why. How shall I do? There is nothing shown in debugging also.

@IBAction func cancel(sender: AnyObject)
{
    NSLog("cancel");
}

Edit: Below is my codes in alertVC which view is to be added to my current view. When I add subview with loading nib, I got this error. How shall I do?

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<_TtC13FastPhoneCard9ContactVC 0x17d53110> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key vwTwoBtnWithTitle.'

import UIKit

class alertVC: UIViewController {

var alertType: NSString = NSString()

@IBOutlet var vwTwoBtnWithTitle: UIView


override func viewDidLoad() {
    super.viewDidLoad()
    setUpInterface()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

class func getView() -> alertVC {
    return NSBundle.mainBundle().loadNibNamed("alertVC", owner: nil, options: nil)[0] as alertVC
}

func setUpInterface()
{
    self.view.backgroundColor = UIColor (white: 1, alpha: 0.5)
    if(alertType == Helper.Stored.alertViewWithPlacehlder)
    {
        addVWTwoBtnWithTitle()
    }
}

func setUpData(input : NSString)
{
    alertType = input
}

func addVWTwoBtnWithTitle()
{
    vwTwoBtnWithTitle.frame = CGRectMake(20, 190, 280, 160);
    vwTwoBtnWithTitle.layer.borderWidth = 1
    vwTwoBtnWithTitle.layer.borderColor = UIColor.blackColor().CGColor
    vwTwoBtnWithTitle.layer.cornerRadius = 5
    self.view.addSubview(vwTwoBtnWithTitle)
}


@IBAction func cancel(sender: AnyObject)
{
    NSLog("cancel");
}


}

Solution

  • You should not do this.This is very wrong practice to add views of other viewControllers as subViews.Every ViewController should manage there own views and its own LifeCycle.

    There are two ways you can do:

    1)Use Xib for views you need in both viewControllers and load the xib whenerver needed.Pass the data back and forth with properties and delegate.

    2)Use ViewController Containers.It will more appropriate to use viewControllers and they manage and structured code well.

    Every viewController should manage their own views and if you need another viewController you should present or navigate that ViewController if that is relevant.Every ViewController have their own life-cycle.

    For Xib

    Create New Xib and Create new file which is subclass of view in your case AlertView than load the view Important:Go to xib -> Identity Inspector -> Custom Class section and Put Class as AlertView

         var nib:Array = NSBundle.mainBundle().loadNibNamed("View", owner: self, options: nil) //View is name of  xib 
          var view = nib[0] as? AlertView
         self.view.addSubview(view)  //this will add subview of current viewController