0

这是我的相关代码

这是我的 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 中的方法没有任何反应。我究竟做错了什么?

4

2 回答 2

1

我发现了我自己的问题。

我试图在 AddSiteViewControler 的 viewDidLoad 方法中设置委托,其中没有 changedaysViewController 类。

我删除了

daysoftheweekViewController *daysvc=[[daysoftheweekViewController alloc]init];
        daysvc.deligate=self;

从我的 viewdidLoad 并添加了这个

controller.deligate=self;

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"daysoftheweek"]){
        daysoftheweekViewController *controller=[segue destinationViewController];
        controller.daysOfTheWeek=daysOfTheWeek;
        controller.daysOfTheWeekNames=daysOfTheWeekNames;
        controller.deligate=self;
    }

}

感谢所有的评论

于 2013-06-06T12:46:42.497 回答
0

试试 @property (nonatomic, retain) id<changedays>deligate;你的 changeDaysViewController.h & @synthesize deligatechangeDaysViewController.m

于 2013-06-06T05:23:14.767 回答