2016-10-13 8 views
0

Привет, у меня есть это сомнение, могу ли я закончить контроль обновления в другом классе? В моей tableviewcontroller у меня есть это:Как я могу завершить обновление в другом классе?

func setRefreshGesture() { 
let refreshGesture = UIRefreshControl() 
refreshGesture.backgroundColor = UIColor.clearColor() 
refreshGesture.tintColor = UIColor.whiteColor() 
refreshGesture.addTarget(WorkUpdater.sharedInstance,action:#selector(WorkUpdater.attemptUpdateControl),forControlEvents:.ValueChanged)  
self.refreshControl = refreshGesture 
self.refreshControl?.layer.zPosition = self.tableView.backgroundView!.layer.zPosition + 1 
    } 

и в моем другом классе .. есть это:

func attemptUpdateControl(){ 
     if !isUpdating && canPerformUpdate() { 
      isUpdating = true 
      performUpdateControl() 
     } else { 
      print("ERROR: - Update in progress") 
     } 
    } 

func performUpdateControl(){ 
     let reach = Reachability.reachabilityForInternetConnection()! 
     if reach.isReachable() { 
     work.getData() 
     } else { 
      UIAlertView(title: "Device without connection", message: "You must have an internet connection to use this feature", delegate: nil, cancelButtonTitle: "OK").show() 

// здесь я хочу закончить свое обновление в tableviewcontroller

}

}

ответ

0

Вы можете либо настроить таможню Delegate или используйте NSNotifcationCenter. Я не уверен, каковы ваши отношения TableViewController с WorkUpdater.

Поэтому я показываю вам возможный способ сделать это с помощью NSNotifactionCenter. В вашем TableViewController, есть это в viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.refreshCompleted), name:"refreshCompleted", object: nil) 

Тогда есть refreshCompleted FUNC:

func refreshCompleted() { 
    refreshGesture.endRefreshing()  //you have to set it as a class var 
} 

также обновит WorkUpdater с этим:

func performUpdateControl(){ 
    let reach = Reachability.reachabilityForInternetConnection()! 
    if reach.isReachable() { 
    work.getData() 
    } else { 
     UIAlertView(title: "Device without connection", message: "You must have an internet connection to use this feature", delegate: nil, cancelButtonTitle: "OK").show() 
    } 
    //add this   
    NSNotificationCenter.defaultCenter().postNotificationName("refreshCompleted", object: nil) 
}