2017-05-26 6 views
-2

TableViewのセルボタンから別のビューコントローラに値を渡したいと思います。私は、TableViewのセルにボタンを持っているので、ボタン操作で値を渡している間に問題に直面しています。私は初心者のように詳細を回答してください。テーブルビューのセルから値を渡す方法Swiftを使用してボタンのアクションを他のView Controllerに渡す

+0

を参照してください。 –

答えて

0

デリゲートアプローチを使用して、TableViewCellのボタンクリックでOneViewControllerからOtherViewControllerに値を渡すことができます。

class OneViewController : UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate { 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cellIdentifier = "CustomCell" 
     var cell : CustomCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CustomCell? 
     if (cell == nil) { 

      cell = Bundle.main.loadNibNamed("CustomCell", owner: nil, options: nil)?[0] as? CustomCell 
     } 

     cell?.delegate = self 
     return cell! 
    } 

    buttonClickAtIndex:(_ index : Int) { 

     let value : Any = dataSource[index] 
     let otherViewController : OtherViewController ///Create variable of OtherViewController 
     otherViewController.value 
     /// push OtherViewController 
    } 

} 

protocol CustomCellDelegate : class { 

    func buttonClickAtIndex:(_ index : Int) 
} 

class CustomCell : UITableViewCell { 

IBOutlet weak var button : UIButton! 
weak var delegate : CustomCellDelegate? 

    func buttonClick(_ sender : UIButton) { 

     if let _delegate = delegate { 
     _delegate.buttonClickAtIndex:(sender.tag) 
     } 
    } 

} 

class OtherViewController : UIViewController { 

    var value : Any /// set Type and default value 
} 
0

次回に1つのビューコントローラから渡すseguesにメソッドを使用している場合(準備と呼ばれる方法使用している:送信者:)。セグエがトリガーされますたび、あなたはリンゴの開発者のウェブサイトからデータを格納するためのコードまたは任意のクリーンアップ

例えば提供

この場合には、ソース・ビュー・コントローラ(から送信されるデータオブジェクト(おそらくそのために別々の迅速ファイル)を準備しますMealViewController)。簡単な食事オブジェクトが送信される準備ができています。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 

super.prepare(for: segue, sender: sender) 

// Configure the destination view controller only when the save button is pressed. 
guard let button = sender as? UIBarButtonItem, button === saveButton else { 
    os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug) 
    return 
} 

let name = nameTextField.text ?? "" 
let photo = photoImageView.image 
let rating = ratingControl.rating 

// Set the meal to be passed to MealTableViewController after the unwind segue. 
meal = Meal(name: name, photo: photo, rating: rating) 
} 

次に、これらのコードを受信側に配置します。

@IBAction func unwindToMealList(sender: UIStoryboardSegue) { 
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal { 

     // Add a new meal. 
     let newIndexPath = IndexPath(row: meals.count, section: 0) 

     meals.append(meal) 
     tableView.insertRows(at: [newIndexPath], with: .automatic) 
    } 
} 

ここでは、まずUIViewControllerからMealViewControllerにダウンキャストしようとします。そして、食事のオブジェクトを取得し、食事のテーブルに記入してください。あなたがここで起こっていることすべてを理解していなければ心配しないでください。

重要な部品があります。

1 - ソースビューコントローラで、準備メソッドを作成します。 super.prepareを呼び出し、ここで送信したいオブジェクトを作成します。

2 - 受信側が@IBAction func YOUMETHODNAME(送信者:UIStoryboardSegue)を作成します。ソースビューコントローラにUIViewControllerをダウンキャストし、最初の手順で作成したオブジェクトを取得します。

これが役に立ちます。詳細は

ちょうどcellForRow` `でyourButton.tag`は、あなたのテーブルの列からあなたのボタンアクションとアクセス要素にsender.tag``介して選択されたボタンのタグを取得し、それを渡す `設定 https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html

関連する問題