没有从第二个ViewController中的tableView传递值?

时间:2016-05-02 02:07:25

标签: ios uitableview uiviewcontroller swift2

我在stackoverflow中读了其他帖子以找到答案,但我找不到答案为什么我无法在第二个ViewController中获得传递值。

在第一个ViewController中,我生成了tableView值:

func loadInitialData() {
    todoItems = [
        TodoItem(itemName: "www.google.com"),
        TodoItem(itemName: "www.bing.com")
    ]
}

并在tableView中我生成发送参数到第二个ViewController:

var valueToPass:String!

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)

    let tappedItem = todoItems[indexPath.row] as TodoItem
    let linkURL: String! = todoItems[indexPath.row].itemName
    tappedItem.completed = !tappedItem.completed

    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
    valueToPass = linkURL
    print("value to pass:"+valueToPass)
    performSegueWithIdentifier("lemparURL", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "lemparURL") {
        // initialize new view controller and cast it as your view controller
        let svc = segue.destinationViewController as! ViewController
        // your new view controller should have property that will store passed value
        print("sent value:"+valueToPass)
        svc.kirimURL = valueToPass
    }
}

我从tableView打印传递了值,我也从prepareForSeque print中发送了值。

在第二个ViewController中我尝试获取参数:

@IBOutlet weak var searchbar: UITextField!
var todoItem: TodoItem = TodoItem(itemName: "")
var kirimURL :String!
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (self.searchbar.text != "") {
        self.todoItem = TodoItem(itemName: self.searchbar.text!)
    }
}

在viewDidLoad中我添加:

self.searchbar.delegate = self;

并在viewDidAppear中我对此进行编码以获取参数:

super.viewDidAppear(animated)
self.searchbar.delegate = self;
print("passed value from first viewController:"+kirimURL)

但我没有得到kirimURL值(传递值)。 lemparURL seque已经正确了。如何纠正这个?

如果直接从func tableView发送参数我添加:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController1 = storyboard.instantiateViewControllerWithIdentifier("searchview") as! ViewController
viewController1.kirimURL = valueToPass
self.presentViewController(viewController1, animated: true , completion: nil)

0 个答案:

没有答案