Ben Five people learn iOS: Notify all UI Updates

Source: Internet
Author: User

I haven't written anything for a long time, so I'll leave a note today.

After all, not fully understand the working mechanism of iOS, hastily use engineering to practiced hand, some relatively simple problems, solve or take some detours.

In my project, there is the code behind the background work, the background work will need to notify the foreground all page refresh, initially I use a custom protocol to deal with, theoretically feasible, I will all need to update the interface to implement the refresh protocol, and then add the interface instance into an array, Iterate through the array when you need to update the interface, notifying you.

Protocol mirefresh{   func Refresh ()}

var list = [Mirefresh] ()//all interfaces

For x in list {  X.refresh ()}

Interface Implementation Mirefresh

Class Listcontroller:uitableviewcontroller, mirefresh{   func refresh () {println ("Update")}}

Add itself to the list when the interface starts

    Override Func Viewdidload () {        super.viewdidload ()        list.append (self)    }

In fact, this method can be implemented refresh, but will cause the list to add duplicate objects, I try to detect whether the list contains the implementation of the Protocal object, but always not its method.


Toss the majority of days, in the group consulting also did not get effective solution, finally through a predecessor, using Notificationcenter to solve, in fact, Notificationcenter is also very simple

Add observer identity to the interface and specify the method to invoke

Func Regrefresh () {        nsnotificationcenter.defaultcenter (). Addobserver (Self, selector: "Refresh", Name: "Refresh", Object:nil)    }

Called when a notification is required

Func Notifyall () {        nsnotificationcenter.defaultcenter (). Postnotificationname ("Refresh", Object:nil)    }
Thank you for your help, in some notes, also want to be able to help others.

Ben Five people learn iOS: Notify all UI Updates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.