更改视图时,为什么我的按钮首选项/参数会重置?

时间:2015-10-25 14:30:21

标签: ios swift uiview uiviewcontroller uibutton

我有这个iOS应用程序,其中tableview中的单元格的值用于设置我的UIButtons上的首选项/参数。

当按下按钮时,我将标签ID发送到桌面视图,当用户按下一个单元格时,它会同时收集文本和图像,并将其(连同标签ID)返回到主视图。

这会成功更改按钮上的参数以及相应的按钮标签ID,但是当我现在按下一个新按钮执行相同的操作时,它将重置第一个按钮更改(清除图像和文本)并仅将更改应用于按下的新按钮。

这是主视图控制器类:

class ViewController: UIViewController {

var recievedItem: ChosenItem?
var imageToButton: UIImage?

@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!

func AddNew() {

    performSegueWithIdentifier("addNew", sender: nil)
}

@IBAction func loadItem(sender: UIButton!) {


    performSegueWithIdentifier("itemList", sender: sender)

}

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

    if(segue.identifier == "itemList"){

        let tableViewController : TableViewController = segue.destinationViewController as! TableViewController
        tableViewController.buttonTag = sender!.tag

    }

}


@IBAction func play(sender: UIButton) {

    print("Jeg har fått \(recievedItem!.chosenWord)")

}

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.translucent = true

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Open", style: .Plain, target: self, action: "AddNew")

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "AddNew")

    if(recievedItem != nil){
        imageToButton = UIImage(data: recievedItem!.chosenImage)

        switch recievedItem!.chosenButton{

        case 0:
            button1.setBackgroundImage(imageToButton, forState: .Normal)
            button1.setTitle(recievedItem!.chosenWord, forState: .Normal)
        case 1:
            button2.setBackgroundImage(imageToButton, forState: .Normal)
            button2.setTitle(recievedItem!.chosenWord, forState: .Normal)
        case 2:
            button3.setBackgroundImage(imageToButton, forState: .Normal)
            button3.setTitle(recievedItem!.chosenWord, forState: .Normal)

        default:
            print("No buttonTag recieved")
        }


    }


}


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

}

这是tableview类:

class TableViewController: UITableViewController {

var words = [Words]()
var chosenItem: ChosenItem!
var buttonTag: Int!

override func viewDidLoad() {
    super.viewDidLoad()

    let appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    let context: NSManagedObjectContext = appDel.managedObjectContext

    let request = NSFetchRequest(entityName: "Words")
    request.returnsObjectsAsFaults = false

    do {
     words = try context.executeFetchRequest(request) as! [Words]

    } catch {
     print("Unresolved error")
     abort()
    }

    print("Her er også button tag \(buttonTag)")
}

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


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

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

    let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let itemWord = self.words[indexPath.row]

    cell.textLabel?.text = itemWord.word

    cell.imageView?.image = UIImage(data: itemWord.image!)

    return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print(self.words[indexPath.row].word!)

    chosenItem = ChosenItem()
    chosenItem.chosenButton = buttonTag
    chosenItem.chosenWord = self.words[indexPath.row].word!
    chosenItem.chosenImage = self.words[indexPath.row].image!
    performSegueWithIdentifier("backToMain", sender: chosenItem)

}

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

    if(segue.identifier == "backToMain"){

        let mainViewController : ViewController = segue.destinationViewController as! ViewController

        let data = sender as! ChosenItem

        mainViewController.recievedItem = data


    }

}

}

这是保存数据的模型类:

class ChosenItem: NSObject {

var chosenButton: Int!
var chosenWord: String!
var chosenImage: NSData!

}

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

performSegueWithIdentifier("backToMain", sender: chosenItem)

那是如何从推送或呈现的视图控制器返回到推送或呈现它的视图控制器!你正在做的是创建一个完全新的视图控制器,这就是为什么没有设置所选按钮的原因;它是一个不同的视图控制器,具有不同的选择按钮,即没有,因为这个视图控制器刚刚出现。

这不仅会使所选按钮搞乱,而且最终您的应用程序会崩溃,因为每次前进和后退时,您都会在另一个上创建一个视图控制器,最终您将耗尽内存。

从推送的视图控制器返回的方法是popViewController。从呈现的视图控制器返回的方法是dismissViewController

如果你知道自己在做什么,你可以使用一种叫做 unwind segue 的特殊非segue segue,但听起来并不像你已经准备好了。< / p>