iPhone 上一个窗口中的两个开关
我刚刚开始学习如何开发 iPhone 应用程序。
我正在尝试制作一个带有两个开关的应用程序。我做了两个类(Switch1 和 Switch2)。 首先,我使用一个开关 (Switch1) 测试了该应用程序,并且该应用程序运行正常。但是当我制作第二个类(Switch2)并构建/运行应用程序时,第一个开关(Switch1)消失了,我看到的只是第二个开关(Switch2)。
之后我制作了(Switch1和Switch2)celarColor的背景,我可以看到两个开关。但是,第一个开关(Switch1)无法切换。
所以我认为我的问题是如何使两个开关(Switch1和Switch2)在“窗口”中可见并同时工作
问题(可能很愚蠢):我怎样才能使它们可见并同时工作? 我认为问题出在以下代码中: 这是来自 AppDelegate
UIScreen *s1 = [UIScreen mainScreen];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
[window addSubview: view1];
[window makeKeyAndVisible];
UIScreen *s2 = [UIScreen mainScreen];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s2.bounds];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
这是 Switch1.h #import
@interface Switch1 : UIView {
UISwitch *mySwitch1;
}
@property (nonatomic, retain) IBOutlet UISwitch *mySwitch1;
@end
这是 Switch1.m
#import "Switch1.h"
@implementation Switch1
@synthesize mySwitch1;
- (id) initWithFrame: (CGRect) frame {
if ((self = [super initWithFrame: frame])) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
mySwitch1 = [[UISwitch alloc] initWithFrame: CGRectZero];
if (mySwitch1 == nil) {
[self release];
return nil;
}
mySwitch1.on = NO; //the default
[mySwitch1 addTarget: [UIApplication sharedApplication].delegate
action: @selector(valueChanged:)
forControlEvents: UIControlEventValueChanged
];
CGRect b1 = self.bounds;
mySwitch1.transform = CGAffineTransformMakeScale(2, 2);
mySwitch1.center = CGPointMake(
b1.origin.x + b1.size.width / 2,
b1.origin.y + b1.size.height / 2
);
[self addSubview: mySwitch1];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect {
// Drawing code
}
*/
- (void) dealloc {
[mySwitch1 release];
[super dealloc];
}
@end
I've just started learning how to develop an iPhone app.
I'm trying to make an app with two switches. I made two classes (Switch1 & Switch2).
First, I tested the app with one switch (Switch1), and the app worked. But when I made the second class (Switch2) and I Build/Run the app, the first switch (Switch1) disappeared, and what I saw just the second switch (Switch2).
After that I made the background of the (Switch1 & Switch2) celarColor, I could see both of switches. However, the first switch (Switch1) can't be switched.
so I think my problem is how to make both switches (Switch1 & Switch2) visible and working at the same time in the "window"
The question (could be stupid): What can I make them visible and working at the same time?
I think the problem in the following code: This is from the AppDelegate
UIScreen *s1 = [UIScreen mainScreen];
view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s1.bounds];
[window addSubview: view1];
[window makeKeyAndVisible];
UIScreen *s2 = [UIScreen mainScreen];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];
window = [[UIWindow alloc] initWithFrame: s2.bounds];
[window addSubview: view2];
[window makeKeyAndVisible];
return YES;
Here is the Switch1.h
#import
@interface Switch1 : UIView {
UISwitch *mySwitch1;
}
@property (nonatomic, retain) IBOutlet UISwitch *mySwitch1;
@end
Here is the Switch1.m
#import "Switch1.h"
@implementation Switch1
@synthesize mySwitch1;
- (id) initWithFrame: (CGRect) frame {
if ((self = [super initWithFrame: frame])) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
mySwitch1 = [[UISwitch alloc] initWithFrame: CGRectZero];
if (mySwitch1 == nil) {
[self release];
return nil;
}
mySwitch1.on = NO; //the default
[mySwitch1 addTarget: [UIApplication sharedApplication].delegate
action: @selector(valueChanged:)
forControlEvents: UIControlEventValueChanged
];
CGRect b1 = self.bounds;
mySwitch1.transform = CGAffineTransformMakeScale(2, 2);
mySwitch1.center = CGPointMake(
b1.origin.x + b1.size.width / 2,
b1.origin.y + b1.size.height / 2
);
[self addSubview: mySwitch1];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void) drawRect: (CGRect) rect {
// Drawing code
}
*/
- (void) dealloc {
[mySwitch1 release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想要使用视图配置视图控制器,然后首先将两个开关放在该视图控制器上。您了解这里的 MVC 模式吗:https: //developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
这是视图控制器指南:https://developer.apple.com/library/ios/#featuredarticles /ViewControllerPGforiPhoneOS/Introduction/Introduction.html。
当您使用 UIScreen 进行初始化时,您将使两个开关具有相同的大小(窗口大小),因此开关 2 优先于开关 1,因为它是第二个初始化的。
You might want to configure a view controller with a view and then toss your two switches on that first. Do you understand the MVC patterns here: https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html
and here's the view controller guide: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html.
When you're initializing with UIScreen, you're making both switches the same size (the window size) and thus switch 2 is over switch 1 since it's initialized second.
因此,您将一个屏幕 (s2) 添加到另一个屏幕 (s1) 之上,因此您无法访问 s1。您需要使 s2 和 s1 的尺寸更小,以便它们不会占据整个屏幕尺寸。
另外,通过 makeKeyAndVisible 可以使窗口可见并能够接受用户交互。没必要说两遍。
So you are adding one screen (s2) on top of another screen (s1) and therefore you are not able to access s1. You need to make the size of s2 and s1 smaller so that they do not take the whole screen size.
Also by saying makeKeyAndVisible you make the window visible and able to accept user interaction. No need to say that twice.
当您调用 s1 时,您正在重置窗口,
因为您已在其上创建了一个新窗口,所以该窗口不再存在。你可以这样做
如果你刚刚开始,一定要看看这个 iPhone 开发教程。它展示了如何使用 UIViewController、UIView 以及许多为 iPhone 提供的类,如 UITableView 和 UIImageView。
You're resetting the window when you call
s1 isn't there anymore because you've created a new window over it. You could just do
If you're just starting, definitely check out this tutorial on iPhone dev. It shows how to use UIViewController, UIView, and a lot of the supplied classes for the iPhone like UITableView and UIImageView.