objective-c 以编程方式创建 UISwitch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2025842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:43:05  来源:igfitidea点击:

Creating a UISwitch Programmatically

iphoneobjective-cuiviewuiswitch

提问by Steve

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:

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

In the AddViewController, I setup the UISwitch:

在 AddViewController 中,我设置了 UISwitch:

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

 }

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

I also setup an IBAction:

我还设置了一个 IBAction:

- (IBAction)flip:(id)sender;

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

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

Edit 1

编辑 1

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

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

   - (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];

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:

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

- (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

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

采纳答案by Ben Gottlieb

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:

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

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

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

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

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

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

回答by Costique

Steve, I'm sorry if I misunderstood your question. Did you actually create the UISwitchand add it to the view hierarchy? Your controller's -loadViewor -viewDidLoadimplementation should have code like the following:

史蒂夫,如果我误解了你的问题,我很抱歉。您是否真的创建了UISwitch并将其添加到视图层次结构中?您的控制器-loadView-viewDidLoad实现应具有如下代码:

// Use the ivar here
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 release];