从类函数返回UIViewController

时间:2017-03-07 08:44:19

标签: ios swift model-view-controller

在我的Restaurant模型中,我有一个返回UINavigationController的函数,我已实现它以使我的代码更具可读性。但我很好奇是否可以这样做,或者我应该使用UIViewController作为参数来实现Restaurant的功能。

func reserveTable(timeIndex: Int) -> UINavigationController {
    let storyBoard = UIStoryboard(name: "Reservations", bundle: nil)
    let targetVC = storyBoard.instantiateViewController(withIdentifier: "bookingNC") as! UINavigationController
    let destinationVC = targetVC.topViewController as! BookingViewController
    destinationVC.availabeleDates = self.schedule
    destinationVC.timeRange = self.avaliableTimes
    destinationVC.restaurantId = self.id
    destinationVC.requestedTimeIndex = timeIndex
    destinationVC.reservationType = "Бронирование"
    return targetVC
}  

1 个答案:

答案 0 :(得分:1)

我会这样做,你的模型应该处理数据,而不是视图。

您可以通过多种方式执行此操作,我个人会在预订控制器上预订房产。然后你可以做类似的事情:

let vc = BookingViewController()
vc.reservation = reservation
self.present(vc, animated: true)

无论您需要出示预订控制器。确保您先预订。

在BookingViewController的init / viewDidLoad方法中,你可以做很多上面的工作......

override func viewDidLoad() {
    super.viewDidLoad()

    if let reservation = self.reservation {
        self.availabeleDates = reservation.schedule
        self.timeRange = reservation.avaliableTimes
        self.restaurantId = reservation.id
        self.requestedTimeIndex = timeIndex
        self.reservationType = "Бронирование"
    }  
}