TableViewのセルボタンから別のビューコントローラに値を渡したいと思います。私は、TableViewのセルにボタンを持っているので、ボタン操作で値を渡している間に問題に直面しています。私は初心者のように詳細を回答してください。テーブルビューのセルから値を渡す方法Swiftを使用してボタンのアクションを他のView Controllerに渡す
答えて
デリゲートアプローチを使用して、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
}
次回に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
- 1. Swift Segueを使用してView Controllerにデータを渡す方法は?
- 2. 他のView Controller(iOS)にタッチを渡す
- 3. View ControllerからSwiftのクラスにメッセージを渡す
- 4. 1つのView Controllerから別のView ControllerにUI ImageViewを渡す方法は? swift 3
- 5. 1つのView Controllerから別のView Controllerへデータを渡す方法
- 6. GETでViewからControllerへ 'string'値を渡す方法
- 7. View Controllerからポップアップビューコントローラ(swift/ios)にデータを渡すには
- 8. Swift 3:TabBarControllerからChild View Controllerにデータを渡す
- 9. ViewDataを使用してASP.NET MVC3のControllerからViewに文字列を渡す
- 10. laravelのView(Blade)からControllerへの配列を使用してデータを渡す
- 11. テーブルビューのセル値を別のページ(uiviewcontroller)に渡す方法は?
- 12. View Controllerからデータを渡すMapKit
- 13. View ControllerからNSObjectへデータを渡す
- 14. Swift 3.0 Segueを使用してView Controller間で情報を渡す
- 15. Swift:ユーザーがTableviewセルをタップすると、あるView Controllerから別のView Controllerへデータをシフトする方法
- 16. f:checkboxを使用してJSPからControllerに値を渡すチェックボックス
- 17. 2番目のView Controllerにデータソースを渡すには? Swift
- 18. ViewからControllerへのモデルデータの受け渡しとその値の使用
- 19. View Controllerからデータを渡す方法は?
- 20. テーブルビューのセルからデータを渡す方法
- 21. UIImageを別のView Controllerに渡す
- 22. Swift 3とObj Cを使用してNSNotificationCenterでApp DelegateからView Controllerに通知データを渡す
- 23. SWrevaelviewController swiftを使用して、テーブルビューの特定の行から別のビューコントローラにデータを渡す方法3
- 24. 2つのセグを同じView Controllerに渡す方法は?
- 25. Swift 3.0でボタンを使用して、ViewControllerから別のViewControllerに値を渡す方法
- 26. テーブルビューから別のビューにコアデータ値を渡す方法
- 27. URLから値を取得してアクションに渡す方法は?
- 28. 他のUIViewControllerからView Controllerを削除するSwift iOS
- 29. DetailViewControllerから別のView ControllerにCore Dataオブジェクトを渡す
- 30. ModelアクションをTkinter MVCのViewボタンに渡すには?
を参照してください。 –