单击数据源中的按钮时如何调用函数? Objective-C
我的问题看起来很简单,但我无法得到答案。我在 TableItemCell 子类中创建了一个 UiSwitch,我希望他从我的 tableviewcontroller 中调用一个函数(在我的例子中关闭)。
如何从 TableItemCell 的子类访问此函数?
这是我的代码:
@implementation CCSettingsTableItemCell
@synthesize idSetting;
//Overriding cell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)identifier {
if ((self = [super initWithStyle: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
}
我的主控制器代码如下:
@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
澄清:数据源添加了我的自定义单元格类型 CCSettingsTableItem 的对象
任何帮助或提示都会很棒!
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 initWithStyle: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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用@protocol来声明您的“dismiss”方法,该方法由您的SettingController实现。使SettingController 类符合CCSettingsTableItemCell。
否则,在CCSettingsTableItemCell 的.h 文件中创建SettingController 的对象。使用此对象来调用“dismiss”方法。
You can use @protocol which declares your "dismiss" method which is implemented by your SettingController. Conform SettingController class to CCSettingsTableItemCell.
Otherwise create an object of SettingController in the .h file of CCSettingsTableItemCell. Use this object to call "dismiss" method.