开发者

How to call a function when button from datasource clicked ? Objective-C

My problem seems simple but i can't get the answer. I have a UiSwitch created in my TableItemCell subclass and I want him to call a function (dismiss in my case) from my tableviewcontroller that is instatiate.

How do I access this function from my subclass of TableItemCell ?

This is my code :

@implementation CCSettingsTableItemCell
@synthesize idSetting;

//Overriding cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
    if ((self = [super initWithSty开发者_JAVA百科le:UITableViewCellStyleValue2 reuseIdentifier:identifier])) {
        switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
        self.accessoryView = switchView;
        [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [switchView release];
    }
    return self;
}

- (void) switchChanged:(id)sender {
    UISwitch* switchControl = sender;

    /////////////////////////////////////////////////////////
    //Here i want to call my function from my main controller

}

And my main controller code is as this :

@implementation SettingController

///////////////////////////////////////////////////////////////////////////////////////////////////
// private

// This is the function i want to call
- (void)dismiss {
    [self dismissModalViewControllerAnimated:YES];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    random code;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// TTTableViewController

- (void)loadView {
    random code;
}

- (void)createModel {
    self.dataSource = [SettingControllerDataSource viewDataSource];

    // If dataSource nil, show an empty Message
    if (self.dataSource == nil) {
        [self showEmpty:YES];
    }
}

@end

Clarification : the datasource adds object of my customcell type CCSettingsTableItem

Any help or hints would be great !


  1. You can use @protocol which declares your "dismiss" method which is implemented by your SettingController. Conform SettingController class to CCSettingsTableItemCell.

  2. Otherwise create an object of SettingController in the .h file of CCSettingsTableItemCell. Use this object to call "dismiss" method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜