iPhone 上一个窗口中的两个开关

发布于 2024-11-25 17:30:18 字数 2142 浏览 2 评论 0原文

我刚刚开始学习如何开发 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 技术交流群。

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

发布评论

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

评论(3

长途伴 2024-12-02 17:30:18

您可能想要使用视图配置视图控制器,然后首先将两个开关放在该视图控制器上。您了解这里的 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.

因为看清所以看轻 2024-12-02 17:30:18

因此,您将一个屏幕 (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.

草莓味的萝莉 2024-12-02 17:30:18

当您调用 s1 时,您正在重置窗口,

window = [[UIWindow alloc] initWithFrame: s2.bounds];

因为您已在其上创建了一个新窗口,所以该窗口不再存在。你可以这样做

UIScreen *s1 = [UIScreen mainScreen];
window = [[UIWindow alloc] initWithFrame: s1.bounds];

view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];

[window addSubview: view1];
[window addSubview: view2];

[window makeKeyAndVisible];

return YES;

如果你刚刚开始,一定要看看这个 iPhone 开发教程。它展示了如何使用 UIViewController、UIView 以及许多为 iPhone 提供的类,如 UITableView 和 UIImageView。

You're resetting the window when you call

window = [[UIWindow alloc] initWithFrame: s2.bounds];

s1 isn't there anymore because you've created a new window over it. You could just do

UIScreen *s1 = [UIScreen mainScreen];
window = [[UIWindow alloc] initWithFrame: s1.bounds];

view1 = [[Switch1 alloc] initWithFrame: s1.applicationFrame];
view2 = [[Switch2 alloc] initWithFrame: s2.applicationFrame];

[window addSubview: view1];
[window addSubview: view2];

[window makeKeyAndVisible];

return YES;

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.

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