iOS Development: Use proxy mode to listen for switch status change events

Source: Internet
Author: User
Tags uikit

Remember to troubleshoot cross-controller monitoring switch state changes.

To unify the contents of the UITableViewCell, a base class of the UITableViewCell class is customized, named Settingcell. The content shown in Settingcell is provided by the data Model Settingitem: Define an attribute in Settingcell.

@property (nonatomic, strong) Settingitem *item;

The subclass of several settingitem is then defined to represent the cell (1) that displays the different contents. Because all the switching status of the archive and reconciliation files are the same, so unified in the parent class Settingitem implementation. However, after clicking "Gesture password" to jump to the next control (2), it is necessary to monitor the state of the gesture password switch separately, which involves the problem of monitoring switch state change events across the controller. I first thought of the proxy mode.

Fig. 1 Fig. 2

First, declare the proxy methods and properties in SettingCell.h:

1 #import<UIKit/UIKit.h>2 3 @classSettingitem, Settingcell;4 5 @protocolSettingcelldelegate <NSObject>6 7 @optional8 9 //Monitor switch status changeTen- (void) Settingcell: (Settingcell *) cell switchchanged: (Uiswitch *) SwitchView; One  A @end -  - @interfaceSettingcell:uitableviewcell the  - //Storing model data -@property (nonatomic, strong) Settingitem *item; -  +@property (nonatomic, assign, getter =islastrowinsection) BOOL lastrowinsection; -  ++ (Instancetype) Cellwithtableview: (UITableView *) TableView; A  at //Defining Proxy Properties -@property (nonatomic, weak)ID<SettingCellDelegate>Delegate; -  - @end

Then, initialize the switch in SETTINGCELL.M and register the valuechanged event, and invoke the proxy method in the Switchstatechange: method to pass the switch state:

1-(Uiswitch *) SwitchView2 {3     if(_switchview = =Nil) {4_switchview =[[Uiswitch alloc] init];5 [_switchview addtarget:self Action: @selector (switchstatechange:) forcontrolevents:uicontroleventvaluechanged ];6     }7     return_switchview;8 }9 Ten /** One * Monitor switch status change A  */ -- (void) Switchstatechange: (Uiswitch *) SwitchView - { the //nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults]; - //[Defaults setBool:self.switchView.isOn forKey:self.item.title]; - //[Defaults synchronize]; -     if([Self.Delegaterespondstoselector: @selector (settingcell:switchchanged:)]) { +[Self.Delegatesettingcell:self Switchchanged:switchview]; -     } +      A [corearchive setBool:self.switchView.isOn key:self.item.title]; at}

Finally, the proxy method is implemented in the controller in Figure 2:

1 -(void) Settingcell: (Settingcell *) cell switchchanged: (Uiswitch *) SwitchView2 {3     NSLog (@ " gesture Password Switch state changed-------------------"); 4 }

But found that the click Switch and did not find the results of the printing, distress ~

After checking the usage of the proxy mode, it is found that the proxy for the Settingcell is not set to the current controller (Gestureviewcontroller). The problem again, in the controller in Figure 2 I can not get Settingcell, but have to trace back, found that the controller is also inherited from a custom controller (Basesettingviewcontroller), It is therefore possible to set the proxy as the current controller in the cell initialization method in the parent class controller (line 11th below). So happy to see the print result bird!

1-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath2 {3     //1. Create a cell4Settingcell *cell =[Settingcell Cellwithtableview:tableview];5     6     //2. Passing model data to the cell7Settinggroup *group =Self.data[indexpath.section];8Cell.item =Group.items[indexpath.row];9Cell.lastrowinsection = (Group.items.count-1==indexpath.row);Ten     //Set up proxy OneCell.Delegate=Self ; A     //3. Return to cell -     returncell; -}

Summary ideas:

    • When it comes to cross-controller data access, the agent mode is considered first;
    • When the inheritance of a class is complex, it is necessary to clear the relationship: what is done uniformly in the base class, what is done in the sub-class alone.

iOS Development: Use proxy mode to listen for switch status change events

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.