根据布尔值进入不同的视图控制器
Posted
技术标签:
【中文标题】根据布尔值进入不同的视图控制器【英文标题】:Proceed to different view controllers based on boolean value 【发布时间】:2015-11-27 00:49:10 【问题描述】:当一个表格单元被选中时,如果布尔值为真,我会尝试进入视图控制器,否则(如果为假),则进入另一个视图控制器。任何帮助都会很棒! (布尔值是 isAdmin,如果为 true,则继续进行 eventViewController,否则继续进行 UserEventTableViewController)
项目链接: https://www.dropbox.com/s/1d4d8opuxzpcuk4/TicketekApp.zip?dl=0
代码:
import UIKit
class EventTableViewController: UITableViewController
// MARK: Properties
var events = [Event]()
var isAdmin = false
override func viewDidLoad()
super.viewDidLoad()
// Use the edit button item provided by the table view controller.
navigationItem.leftBarButtonItem = editButtonItem()
// Load any saved events, otherwise load sample data.
if let savedEvents = loadEvents()
events += savedEvents
else
// Load the sample data.
loadSampleEvents()
func loadSampleEvents()
let photo1 = UIImage(named: "event1")!
let event1 = Event(name: "ACDC", photo: photo1, rating: 4, price: 500.0, eventDescription: "Album", album: "Album1")!
let photo2 = UIImage(named: "event2")!
let event2 = Event(name: "Cold Play", photo: photo2, rating: 5, price: 500.0, eventDescription: "Album", album: "Album1")!
let photo3 = UIImage(named: "event3")!
let event3 = Event(name: "One Direction", photo: photo3, rating: 3, price: 500.0, eventDescription: "Album", album: "Album1")!
events += [event1, event2, event3]
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
return 1
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return events.count
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "EventTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! EventTableViewCell
// Fetches the appropriate event for the data source layout.
let event = events[indexPath.row]
cell.nameLabel.text = event.name
cell.photoImageView.image = event.photo
cell.ratingControl.rating = event.rating
cell.priceLabel.text = event.album
return cell
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
// Return false if you do not want item to be editable.
return true
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
if editingStyle == .Delete
// Delete the row from the data source
events.removeAtIndex(indexPath.row)
saveEvents()
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
else if editingStyle == .Insert
// Create new instance of class, add to the array, and add a new row to the table
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
// Return false if you do not want the item to be re-orderable.
return true
*/
// MARK: - Navigation
// preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if segue.identifier == "ShowDetail"
let eventDetailViewController = segue.destinationViewController as! EventViewController
// Get the cell that generated this segue.
if let selectedEventCell = sender as? EventTableViewCell
let indexPath = tableView.indexPathForCell(selectedEventCell)!
let selectedEvent = events[indexPath.row]
eventDetailViewController.event = selectedEvent
print(selectedEventCell)
else if segue.identifier == "AddItem"
print("Adding new event.")
@IBAction func unwindToMealList(sender: UIStoryboardSegue)
if let sourceViewController = sender.sourceViewController as? EventViewController, event = sourceViewController.event
if let selectedIndexPath = tableView.indexPathForSelectedRow
// Update an existing event.
events[selectedIndexPath.row] = event
tableView.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
else
// Add a new event.
let newIndexPath = NSIndexPath(forRow: events.count, inSection: 0)
events.append(event)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
// Save the events.
saveEvents()
// MARK: NSCoding
func saveEvents()
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(events, toFile: Event.ArchiveURL.path!)
if !isSuccessfulSave
print("Failed to save events...")
func loadEvents() -> [Event]?
return NSKeyedUnarchiver.unarchiveObjectWithFile(Event.ArchiveURL.path!) as? [Event]
【问题讨论】:
prepareForSegue
来不及改变segue;您需要从场景中的 UIViewController 对象链接您的 segue,然后实现 didSelectRowAtIndexPath
- 在此方法中,您可以使用 performSegueWithIdentifier
根据您需要的任何逻辑调用所需的 segue
【参考方案1】:
你必须实现 tableView 委托方法didSelectRowAtIndexPath
。
示例如下:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
let index = self.tableView.indexPathForSelectedRow?.row
//use the index to know which cell you selected
//check for your condition here something like
if isAdmin
performSegueWithIdentifier("eventViewControllerSegue", sender: self)
else
performSegueWithIdentifier("userEventTableViewControllerSegue", sender: self)
注意以下几点:
在您的故事板中,将转场绘制到不同的视图控制器并分别分配转场 ID。
在您的代码中,您不会更改 isAdmin 布尔值。也检查一下。
问候。
关于如何从 ViewController 中分离的图片供您参考
【讨论】:
对不起,我是新手,但是当单击表格单元格时,我是否会将 segues 绘制到新的视图控制器,并且会是附件选择吗? 我相信只有在将 segue 从 tableViewCell 绘制到 destinationViewController 时才能看到辅助动作。理想情况下,您应该从 sourceViewController 手动绘制到destinationViewController【参考方案2】:我相信你可以通过使用 UITableViewDelegate 协议中的tableView:didSelectRowAtIndexPath:
来完成你想要的。
将此添加到您的视图控制器
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
并在该函数中处理您的逻辑。
当你准备好进入下一个视图控制器时,使用
performSegueWithIdentifier(identifier: String, sender: AnyObject?)
这是一个例子:
if isAdmin
performSegueWithIdentifier(identifier: "segueToAdminView", sender: self)
else
performSegueWithIdentifier(identifier: "segueToUserEventView", sender: self)
【讨论】:
以上是关于根据布尔值进入不同的视图控制器的主要内容,如果未能解决你的问题,请参考以下文章