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

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

问题描述

在一个看似永无止境的关于iphone开发的更多努力中,我一直在玩苹果开发者网站提供的一些源代码。我正在使用的特定示例是Core Data Books,此处。 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天全站免登陆