以编程方式执行多个segue

时间:2015-10-05 09:46:06

标签: ios iphone swift uiviewcontroller segue

我的应用中有三个视图控制器。我想在我的导航栏中使用按钮切换它们(leftBarButton用于我的第一个ViewController,中央 - 用于第二个,而rightBarButton用于第三个Controller)。因为我希望我的应用程序根据设备屏幕大小调整我的按钮,我需要以编程方式设置它们。

我已经从每个Controller创建了segue,并尝试将它们连接到我的UIViewController类。

这是第一个ViewController的代码(我在其中添加了函数和按钮):

func openExp(sender: AnyObject) {
    self.performSegueWithIdentifier("openPubExp", sender: sender)
}

func openElse(sender: AnyObject) {
    self.performSegueWithIdentifier("openPubElse", sender: sender)
}

我的ViewDidLoad:

var navBarImage: UIImage? = nil
    if UIScreen.mainScreen().bounds.size.width == 375.0 {
        navBarImage = UIImage(named: "publications@2xiphone6.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    }
    else {
        if UIScreen.mainScreen().bounds.size.width == 414.0 {
            navBarImage = UIImage(named: "publications@3x.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        else {
            navBarImage = UIImage(named: "publications@2x.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))

            let button1 = UIButton(type: .Custom) as UIButton
            button1.setImage(UIImage(named: "Moroz.png"), forState: UIControlState.Normal)
            button1.frame = CGRectMake(0, 0, 100, 30)

            button1.addTarget(self, action: "openExp:", forControlEvents: .TouchUpInside)
            let barButton1 = UIBarButtonItem(customView: button1)
            self.navigationItem.leftBarButtonItem = barButton1


            let button2 = UIButton(type: .Custom) as UIButton
            button2.setImage(UIImage(named: "Moroz.png"), forState: UIControlState.Normal)
            button2.frame = CGRectMake(0, 0, 100, 30)

            button2.addTarget(self, action: "openElse:", forControlEvents: .TouchUpInside)
            let barButton2 = UIBarButtonItem(customView: button2)
            self.navigationItem.rightBarButtonItem = barButton2


        }
    }

    self.navigationController?.navigationBar.setBackgroundImage(navBarImage, forBarMetrics: .Default)

我的其他ViewControllers的代码是一样的。我的函数和segue的名称是不同的。我可以在第一个和第二个ViewController之间切换。

问题是每当我尝试访问我的第三个ViewController时,我都会收到此错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

我的第三个ViewController的代码:

var myFeed : NSArray = []
var url: NSURL = NSURL()


@IBOutlet weak var centalPB: UIButton!

override func shouldAutorotate() -> Bool {
    if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
            return true
    }
    else {
        return false
    }
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}

override func viewDidLoad() {
    super.viewDidLoad()


    tableView.estimatedRowHeight = 68.0
    tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.dataSource = self
    self.tableView.delegate = self




    url = NSURL(string: "http://194.186.62.58/AppWebservice/Services.asmx/getOtherNews")!

    loadRss(url);

}

override func viewWillAppear(animated: Bool) {


    super.viewWillAppear(animated)

    self.navigationItem.title = ""
    let backItem = UIBarButtonItem()
    backItem.title = "Назад"
    navigationItem.backBarButtonItem = backItem
    var navBarImage: UIImage? = nil
    if UIScreen.mainScreen().bounds.size.width == 375.0 {
        navBarImage = UIImage(named: "else@2xiphone6.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
    }
    else {
        if UIScreen.mainScreen().bounds.size.width == 414.0 {
            navBarImage = UIImage(named: "else@3x.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        else {
            navBarImage = UIImage(named: "else@2x.png")!.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0))

            let button1 = UIButton(type: .Custom) as UIButton
            button1.setImage(UIImage(named: "Moroz.png"), forState: UIControlState.Normal)
            button1.frame = CGRectMake(0, 0, 210, 40)

            button1.addTarget(self, action: "openElse:", forControlEvents: .TouchUpInside)
            let barButton1 = UIBarButtonItem(customView: button1)
            self.navigationItem.rightBarButtonItem = barButton1

            self.centalPB.frame = CGRectMake(0, 0, 210, 50)
            self.centalPB.setImage(UIImage(named: "Moroz.png"), forState: UIControlState.Normal)
            self.centalPB.addTarget(self, action: "openPublic:", forControlEvents: .TouchUpInside)

        }
    }

    self.navigationController?.navigationBar.setBackgroundImage(navBarImage, forBarMetrics: .Default)
}


func loadRss(data: NSURL) {

    let myParser = XmlParserManager(URL: data)

    myFeed = myParser.feeds

    tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {



    if segue.identifier == "openPage2" {

        let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
        let selectedFTitle: String = myFeed[indexPath.row].objectForKey("Title") as! String
        let selectedFContent: String = myFeed[indexPath.row].objectForKey("Description") as! String

        // Instance of our feedpageviewcontrolelr
        let fpvc: ElseViewController = segue.destinationViewController as! ElseViewController

        fpvc.selectedFeedTitle = selectedFTitle
        fpvc.selectedFeedFeedContent = selectedFContent
    }
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myFeed.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! CustomCellElse

    let chevron = UIImage(named: "Next4.png")
    cell.accessoryType = .DisclosureIndicator
    cell.accessoryView = UIImageView(image: chevron!)
    cell.textLabel?.textColor = UIColor.blackColor()
    // Feeds dictionary.
    var dict : NSDictionary! = myFeed.objectAtIndex(indexPath.row) as! NSDictionary

    cell.titleLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Title") as? String
    cell.descLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Description") as? String
    cell.dateLabel.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_text") as? String

    return cell

}

override func viewDidAppear(animated: Bool) {

    tableView.reloadData()

}

如果我只使用我的故事板就没有错误,但在这种情况下,我无法调整按钮的大小。

更新:

ElseTableViewController 0x7ae5dff0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key centralEB.'

enter image description here

1 个答案:

答案 0 :(得分:1)

这通常是由故事板链接到不存在的属性引起的。 检查故事板中第三个VC到代码文件的所有连接,因为当您尝试加载它时会发生这种连接。

尝试记住您是否更改了属性或方法的任何名称?如果是,请检查您是否续订了连接。