简体   繁体   中英

Iboutlets not working inside block swift

I am fetching a request from server and trying to display it in tablview. In this process I am want to hide the spinner after fetching records. Problem is : Anything associated with self inside block does not work.

@IBOutlet weak var spinner: UIActivityIndicatorView!
@IBOutlet weak var customTableview: CustomTableView!
var widgetArray :NSMutableArray = []
override func viewDidLoad() {
    super.viewDidLoad()

    print(DataObjects.sharedInstance.mainArray)
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

     spinner.hidden = false
    spinner.startAnimating()

    CustomNetworkHit.networkHitForUrl("http://winjitwinds.cloudapp.net/windapi/windapiservice.svc/getcategorydatapagewise?categoryid=1944&pageno=1", completion: {(arrayResult) -> Void in

        // spinner does not stop animating and it does not hide

        self.spinner.stopAnimating()
        self.spinner.hidden = true
        print("Disable the spinner man")
        DataObjects.sharedInstance.mainArray.addObjectsFromArray(arrayResult as [AnyObject])
        self.widgetArray.addObjectsFromArray(arrayResult as [AnyObject])
        self.customTableview.setUpTableView(self.widgetArray)

        // UItableview Does not reload Data.

        self.customTableview.reloadData()


    })

Please specify what you mean by "does not work". Without that it's hard to know for sure but I would guess you are trying to do UI work on a background thread (blocks don't run on the main thread).

Within your block try this:

dispatch_async(dispatch_get_main_queue(), ^(){
    // UI CODE GOES HERE
});

In Swift:

dispatch_async(dispatch_get_main_queue()) {
   // UI CODE GOES HERE
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM