簡體   English   中英

委托不響應方法調用

[英]delegate not responding to method call

這是我相關的代碼

這是我的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上的開關時,我從表視圖方法-(void) updateSwitch登錄到控制台, "is on changed is 1"登錄時"is on changed is 1" 但是,在我的AddSiteViewController中的-(void)didChangeDays方法中什么也沒有發生。 我究竟做錯了什么?

我發現了自己的問題。

我試圖在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;
    }

}

感謝所有的評論

嘗試@property (nonatomic, retain) id<changedays>deligate; 在您的changeDaysViewController.h和@synthesize deligate指定changeDaysViewController.m

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM