在 cellForRowAtIndexPath 中启动时无法获取 UISwitch 的状态?

发布于 2024-11-10 10:22:09 字数 996 浏览 5 评论 0原文

我启动我的 cellForRowAtIndexPath。它出现了,但是当我尝试从不同的类引用它时,它返回(null)。如果我在 viewDidLoad 中启动它,它可以工作,但它需要位于 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: %@", addAlbumViewController.privateSwitch);

请帮忙谢谢。

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: %@", addAlbumViewController.privateSwitch);

Please help Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2024-11-17 10:22:09

假设此代码:

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

实际上在您的 tableView:cellForRowAtIndexPath: 中,那么当您尝试查看 privateSwitch 时,该代码可能甚至没有被调用。 (我假设您在尝试记录之前从 AddAlbumViewController 的新实例中读取了 privateSwitch,但您将该代码遗漏了。)

在 SecondViewController 中,您正在创建 AddAlbumViewController 的新实例并从笔尖加载该视图,但从您发布的代码来看,您实际上并未在屏幕上呈现它(除非您忽略了它?)

另外,顺便说一句,您声明了一个名为 privateSwitch 的属性,但您没有使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文