简体   繁体   English

委托不响应方法调用

[英]delegate not responding to method call

Here are my relevant bits of code 这是我相关的代码

This is my changeDaysViewController.h 这是我的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

This is the method in my changeDaysViewController.m 这是我的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);

}

in my add addsiteViewController.h I have implemented the protocol 在我添加的addsiteViewController.h中,我已经实现了协议

@interface AddSiteViewController : UIViewController <CLLocationManagerDelegate,UITableViewDelegate ,UIActionSheetDelegate,changedays>{...

in the addsiteViewController.m I have 在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;
}

and i have 我有

-(void)didChangeDays:(NSNumber *)position :(BOOL)on_off{
    [daysOfTheWeek replaceObjectAtIndex:[position integerValue] withObject:[NSNumber numberWithBool:on_off]];
    NSLog(@"days of the week changed in delegate");
}

edit 1 here is the cellorrowatindexpath 编辑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;
}

when i change the switch on the UITableView , I get "is on changed is 1" logged on the console from the table view method -(void) updateSwitch . 当我更改UITableView上的开关时,我从表视图方法-(void) updateSwitch登录到控制台, "is on changed is 1"登录时"is on changed is 1" However, nothing happens in the -(void)didChangeDays method that is in my AddSiteViewController. 但是,在我的AddSiteViewController中的-(void)didChangeDays方法中什么也没有发生。 What am I doing wrong? 我究竟做错了什么?

I have found my own problem. 我发现了自己的问题。

I was trying to set the delegate in the viewDidLoad method of AddSiteViewControler, where there was no class of changedaysViewController. 我试图在AddSiteViewControler的viewDidLoad方法中设置委托,那里没有changedaysViewController类。

i removed 我删除了

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

from my viewdidLoad and added this 从我的viewdidLoad并添加了这个

controller.deligate=self;

to

-(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;
    }

}

Thanks for all the comments 感谢所有的评论

Try @property (nonatomic, retain) id<changedays>deligate; 尝试@property (nonatomic, retain) id<changedays>deligate; in your changeDaysViewController.h & @synthesize deligate changeDaysViewController.m 在您的changeDaysViewController.h和@synthesize deligate指定changeDaysViewController.m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM