2014-09-03 3 views
2

Heyhey!Показать новый ViewController после нажатия UIButton в пользовательской ячейке

Я хотел бы показать ViewController с действием (tappedInside UIButton в пользовательской ячейке). В этом случае я не могу просто управлять перетаскиванием из UIButton в пользовательской ячейке в NavigationController (ViewController встроен).

Как я могу понять, что «tappedInside» на кнопке (в пользовательской ячейке) в строке tableview будет отображаться новый ViewController?

Спасибо!

ответ

2

Вы можете вызвать при нажатии на ячейку с делегатом:

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
    // some code 
} 

Чтобы открыть новый viewcontroller, если вы используете раскадровку, вы можете попробовать это:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let newVC = storyboard.instantiateViewControllerWithIdentifier("myIdentifier") as youClassController 
self.presentViewController(newVC, animated: false, completion: nil) 

Надеюсь, что помощь

+0

Я думал, что вопрос состоял в том, чтобы запустить кнопку внутри пользовательской ячейки, не запуская сама ячейку ... –

+0

Я тоже так думал. –

2

С Xcode 6 бета 7, ваши UITableViewController и UITableViewCell подклассы будет выглядеть следующим образом:

import UIKit 

class CustomCell: UITableViewCell { 

    @IBOutlet weak var button: UIButton! 

} 

class TableViewController: UITableViewController { 

    //Set method for UIButton 
    func push(sender: AnyObject) { 
     navigationController!.pushViewController(storyboard!.instantiateViewControllerWithIdentifier("ViewController") as ViewController, animated: true) //where ViewController is the name of the UIViewController and "ViewController" the identifier you set for it in your Storyboard 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomCell 
     cell.selectionStyle = .None 

     //Set button's target 
     cell.button.addTarget(self, action: "push:", forControlEvents: .TouchUpInside) 

     return cell 
    } 

} 
+0

Спасибо за ваш ответ! Я думаю, что нужно добавить Selector перед «push:» cell.button.addTarget (self, action: Selector («push»), forControlEvents: .TouchUpInside) – Joe

+0

С Swift вам больше не нужно. –