Here are my relevant bits of code
This is my 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
-(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
@interface AddSiteViewController : UIViewController <CLLocationManagerDelegate,UITableViewDelegate ,UIActionSheetDelegate,changedays>{...
in the addsiteViewController.m I have
- (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
-(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
. However, nothing happens in the -(void)didChangeDays
method that is in my AddSiteViewController. 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.
i removed
daysoftheweekViewController *daysvc=[[daysoftheweekViewController alloc]init];
daysvc.deligate=self;
from my viewdidLoad and added this
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;
in your changeDaysViewController.h & @synthesize deligate
changeDaysViewController.m
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.