检查是否按下按钮以分割正确的数据

时间:2015-04-01 05:45:11

标签: ios swift segue

嗨,这可能是一个简单的问题,如果它已经在这里我道歉,我搜索了我能找到的但却无法找到适合我具体情况的答案。

这是问题所在。我目前正在创建一个文本冒险,在Xcode 6中使用swift练习一些iOS开发,在这种情况下,我在视图控制器中有一个文本视图,可以滚动到自身的底部和屏幕底部的2个按钮。关键是,如果你点击一个按钮,它会将你分成不同的场景,并且还会改变我在项目中定义的类UserCharacter中的一些一般角色统计数据,这些数据会改变一些基本数据,反之亦然。

事情在我的segue我需要检查用户按下哪个按钮,所以我知道要删除哪些数据但是不能完全弄清楚如何做到这一点。

这是我到目前为止所做的,(我之前已经传过我的数据,但不需要做出选择,所以这很简单)

class RouteZeroViewController: UIViewController {

var character : UserCharacter?

@IBAction func choiceOneButton(sender: AnyObject) {
    character?.strength += 3
    character?.intelligence -= 2
}
@IBAction func choiceTwoButton(sender: AnyObject) {
    character?.agility += 2
    character?.intelligence += 2
    character?.courage -= 2
}
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    // set initial textView load at the top instead of middle/bottom
    self.textView.scrollRangeToVisible(0)
}

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

// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // Get the new view controller using [segue destinationViewController].
    // check for button one push ????????
    var thirdSceneA = segue.destinationViewController as RouteOneAViewController
    thirdSceneA.character = character

    // check for button two push ????????
    var thirdSceneB = segue.destinationViewController as RouteOneBViewController
    thirdSceneB.character = character
    }

}

任何帮助将不胜感激。谢谢..

1 个答案:

答案 0 :(得分:0)

您需要将视图控制器中的segue添加到viewcontroller,请参阅以下链接:IOS7, Segue and storyboards - How to create without a button?

将标签属性设置为每个按钮,即1和2

按钮点击传递按钮

   @IBAction func choiceOneButton(sender: AnyObject) 
{
character?.strength += 3
character?.intelligence -= 2
 self.performSegueWithIdentifier("identifer", sender: sender)
}


 @IBAction func choiceTwoButton(sender: AnyObject)
{
character?.agility += 2
character?.intelligence += 2
character?.courage -= 2
self.performSegueWithIdentifier("identifer", sender: sender)
 }

准备segue方法检查

 override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) 
{
   var btn:UIButton=sender as UIButton

        if btn.tag==1
        {
            // First button clicked
        }

        if btn.tag==2
        {
            // Second button clicked
        }

}