委托不响应方法调用

时间:2013-06-05 21:46:18

标签: iphone objective-c delegates

以下是我的相关代码

这是我的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);

}

在我的add addViewViewController.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登录控制台。但是,我的AddSiteViewController中的-(void)didChangeDays方法没有任何反应。 我做错了什么?

2 个答案:

答案 0 :(得分: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;
    }

}

感谢所有评论

答案 1 :(得分:0)

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