以编程方式创建 UISwitch [英] Creating a UISwitch Programmatically

查看:21
本文介绍了以编程方式创建 UISwitch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了了解有关 iphone 开发的更多信息,我似乎永无止境,我一直在玩一些可通过苹果开发者网站获得的源代码.我正在使用的特定示例是 Core Data Books,在 here.DetailViewController 和 AddViewController 是以编程方式制作的,因为它们没有任何 xib 文件.我的问题是关于在不使用 IB 的情况下以编程方式向视图添加内容.我想在 UITableView 下放置一个 UISwitch,它包含有关 DetailView 中特定书籍的详细信息.我该怎么做呢?这是我迄今为止尝试过的:

In a seemingly never ending effort to learn more about iphone development, I have been playing around with some of the source code available through apples developer website. The particular example I am working with is Core Data Books, found here. The DetailViewController, and the AddViewController are made programatically, because there aren't any xib files for them. My question is about programatically adding things to a view without using IB. I want to put a UISwitch beneath the UITableView that has the detailed information about a particular book in the DetailView. How do I do this? This is what I have tried so far:

在 AddViewController 中,我设置了 UISwitch:

In the AddViewController, I setup the UISwitch:

@interface AddViewController : DetailViewController {
id <AddViewControllerDelegate> delegate;
UISwitch *onoff;

 }

@property (nonatomic, assign) id <AddViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UISwitch *onoff;

我还设置了一个 IBAction:

I also setup an IBAction:

- (IBAction)flip:(id)sender;

然后我在 AddViewController.m 文件中合成它,但没有任何反应.我只需要设置开关并制作它,以便我可以通过我设置的 IBAction 控制它的功能.我知道这非常简单,但我无法弄清楚.所以,任何帮助将不胜感激!谢谢

Then I synthesize it in the AddViewController.m file, but nothing happens. I just need to setup the switch and make it so that I can control what it does from my IBAction that I setup. I know this is embarrassingly simple, but I can't figure it out. So, any help would be appreciated! Thanks

编辑 1

所以我按照我在 viewDidLoad 中的指示实现了代码,如下所示:

So I implemented the code as I was directed to in the viewDidLoad, like so:

   - (void)viewDidLoad {

[super viewDidLoad];    
  UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
[onoff addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
// Set the desired frame location of onoff here
[self.view addSubview: onoff];

它抛出两个警告,说'onoff'的本地声明隐藏了实例变量.但是即使有这些收益,UISwitch 弹出也很好,但是当我移动它或使用它时,它看起来并没有完全正常工作.对于我这样的操作:

And it throws two warnings saying that the local declaration of 'onoff' hides the instance variable. But even though there are those earnings, the UISwitch pops up just fine, but when I move it, or use it, it doesn't look like it is working fully. For my action that looks like this:

- (IBAction)flip:(id)sender {
if (onoff.on) NSLog(@"On");  
else  NSLog(@"Off");  
 }

每当开关打开时,控制台应该读取,当它关闭时,控制台应该读取.对?每当我移动它时,它都会在控制台中重复,关闭.如果它打开或关闭,它只会显示出来.我到底做错了什么?请帮忙!谢谢

Whenever the switch is on, the console should read on, and when its off, the console should read off. Right? Anytime I move it, it just repeats in the console, off. If its on, or if its off, it only shows off. What in the world am I doing wrong? Please help! Thanks

推荐答案

编译器正试图帮助你.您正在覆盖 viewDidLoad; 中的 onoff 实例变量,因此永远不会设置.在您的 -flip: 方法中,您引用了一个 nil 控制器.有两种方法可以解决此问题:

The compiler is trying to help you out. You're overriding the onoff instance variable in your viewDidLoad; thus, that's never getting set. In your -flip: method, you're referencing a nil controller. There are two ways to fix this:

(a) 去掉 onoff 的局部声明,只使用你的实例变量

(a) Get rid of the local declaration of onoff, and just use your instance variable

(b) 将 sender 参数转换为 -flip: 作为 UISwitch,并访问它:

(b) Cast the sender argument to -flip: as a UISwitch, and access that:

- (IBAction) flip: (id) sender {
    UISwitch *onoff = (UISwitch *) sender;
    NSLog(@"%@", onoff.on ? @"On" : @"Off");
}

这篇关于以编程方式创建 UISwitch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆