开发者

Can't get state of UISwitch when initiated in cellForRowAtIndexPath?

I initiate my cellForRowAtIndexPath. It shows up, but when I try to reference it from a different class, it returns (null). If I initiate it in my viewDidLoad, it works but it needs to be in the UITableView.

AddAlbumViewController:

// .h
IBOutlet UISwitch *privateSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *privateSwitch;

// .m
@synthesize privateSwitch;

privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:privateSwitch];

SecondViewController:

// .m

TableViewAppDelegate *dataCeter = (TableViewAppDelegate *)[[UIApplication sharedApplication] delegate];
AddAlbumViewController *addAlbumViewController = [[AddAlbumViewController alloc] initWithNibName:@"AddAlbum" bundle:[NSBundle mainBundle]];
UIView *tempView = addAlbumViewController.view;

NSLog(@"privateSwitch: %@", addAlbumViewContr开发者_运维技巧oller.privateSwitch);

Please help Thanks.


Assuming this code:

privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:    UIControlEventTouchUpInside];
[cell.contentView addSubview:privateSwitch];

is actually in your tableView:cellForRowAtIndexPath:, then that code probably hasn't even been called when you're trying to look at privateSwitch. (I'm assuming you read privateSwitch from your new instance of AddAlbumViewController before you try to log it, but you left that code out.)

In your SecondViewController, you are creating a new instance of the AddAlbumViewController and loading that view from the nib, but from the code you posted you haven't actually presented it on screen (unless you left that out?)

Also, btw, you declared a property called privateSwitch but then you're not using it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜