这是我的相关代码
这是我的 changeDaysViewController.h
@protocol changedays <NSObject>
-(void)didChangeDays:(NSNumber *)position :(BOOL )on_off;
@end
#import "AddSiteViewController.h"
#import <UIKit/UIKit.h>
@interface daysoftheweekViewController : UITableViewController{
}
@property (retain, nonatomic)NSMutableArray *daysOfTheWeek;
@property (retain, nonatomic)NSMutableArray *daysOfTheWeekNames;
@property (assign) id<changedays>deligate;
@end
这是我的 changeDaysViewController.m 中的方法
-(void)updateSwitch:(UIControl*)button withEvent:(UIEvent* )Event{
UISwitch *swch=(UISwitch *)button;
UITableViewCell *cell=(UITableViewCell*)swch.superview;
if ([swch isOn]) {
NSLog(@" is onchanged is %d",swch.tag);
[self.deligate didChangeDays:[NSNumber numberWithInt:cell.tag] :YES];
}else{
NSLog(@" id off row changed is %d",swch.tag);
[self.deligate didChangeDays:[NSNumber numberWithInt:cell.tag] :NO];
}
[self.deligate didChangeDays:[NSNumber numberWithInt:cell.tag] :NO];
NSLog(@" row changed is %d",swch.tag);
}
在我添加 addsiteViewController.h 我已经实现了协议
@interface AddSiteViewController : UIViewController <CLLocationManagerDelegate,UITableViewDelegate ,UIActionSheetDelegate,changedays>{...
在 addsiteViewController.m 我有
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
daysoftheweekViewController *daysvc=[[daysoftheweekViewController alloc]init];
daysvc.deligate=self;
}
return self;
}
我有
-(void)didChangeDays:(NSNumber *)position :(BOOL)on_off{
[daysOfTheWeek replaceObjectAtIndex:[position integerValue] withObject:[NSNumber numberWithBool:on_off]];
NSLog(@"days of the week changed in delegate");
}
编辑 1 这里是 cellorrowatindexpath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row=[indexPath row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if(cell==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UISwitch *sw= [[UISwitch alloc]initWithFrame:CGRectMake(200.0f, 5.0f, 75.0f, 30.0f)];
cell.accessoryView=sw;
sw.tag=row;
[sw addTarget:self action:@selector(updateSwitch:withEvent:) forControlEvents:UIControlEventValueChanged];
// }
cell.textLabel.text=[daysOfTheWeekNames objectAtIndex:row];
if([[daysOfTheWeek objectAtIndex:row]integerValue]==1){
UISwitch *sw=(UISwitch *)cell.accessoryView;
[sw setOn:YES];
}else{
UISwitch *sw=(UISwitch *)cell.accessoryView;
[sw setOn:NO];
}
return cell;
}
当我更改开关时UITableView
,我"is on changed is 1"
从表视图方法登录到控制台-(void) updateSwitch
。但是,-(void)didChangeDays
我的 AddSiteViewController 中的方法没有任何反应。我究竟做错了什么?