【发布时间】:2010-11-27 10:45:14
【问题描述】:
请帮帮我 我是 iphone 开发的新手,这是我的第二个示例代码。我正在尝试将子视图添加到视图并根据所触摸的视图/子视图生成事件。我已经在主视图中添加了子视图,并且所有视图都一次显示。我正在试验的项目是一个新创建的、干净的 Window-Base 应用程序。
我将以下代码写入唯一的 viewController 代码中:
@interface testViewController : UIViewController {
IBOutlet UIView *blueView1;
IBOutlet UIView *blueView2;
: :
IBOutlet UIView *blueView6;
}
//---将插座公开为属性---
@property(非原子,保留)IBOutlet UIView *blueView1;
@property(非原子,保留)IBOutlet UIView *blueView2;
: : *blueView6;
//---声明动作--- -(IBAction) viewClicked: (id) sender;
@结束
它的 .m 文件包含(我在其中附加子视图的 loadView 方法。它们一次都显示得很好。我想要做的是当我的视图/子视图被触摸时,它们会生成一个事件应该根据他们的对象通过这个单一的方法来处理,这将相应地改变视图/子视图的背景颜色。
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
self.view.backgroundColor = [UIColor greenColor];
// 创建一个简单的蓝色方块
CGRect blueFrame0 = CGRectMake(5, 115, 100, 100);
UIView *blueView0 = [[UIView alloc] initWithFrame:blueFrame0];
blueView0.backgroundColor = [UIColor blueColor];
// 创建一个简单的蓝色方块
CGRect blueFrame1 = CGRectMake(110, 115, 100, 100);
UIView *blueView1 = [[UIView alloc] initWithFrame:blueFrame1];
blueView1.backgroundColor = [UIColor blueColor];
// 创建一个简单的蓝色方块
CGRect blueFrame2 = CGRectMake(215, 115, 100, 100);
UIView *blueView2 = [[UIView alloc] initWithFrame:blueFrame2];
blueView2.backgroundColor = [UIColor blueColor];
//--------------------------------------------- -------------------------------------------------- ------------
// 创建一个简单的蓝色方块
CGRect blueFrame3 = CGRectMake(5, 220, 100, 100);
UIView *blueView3 = [[UIView alloc] initWithFrame:blueFrame3];
blueView3.backgroundColor = [UIColor blueColor];
// 创建一个简单的蓝色方块
CGRect blueFrame4 = CGRectMake(110, 220, 100, 100);
UIView *blueView4 = [[UIView alloc] initWithFrame:blueFrame4];
blueView4.backgroundColor = [UIColor blueColor];
// 创建一个简单的蓝色方块
CGRect blueFrame5 = CGRectMake(215, 220, 100, 100);
UIView *blueView5 = [[UIView alloc] initWithFrame:blueFrame5];
blueView5.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueView0];
[self.view addSubview:blueView1];
[self.view addSubview:blueView2];
[self.view addSubview:blueView3];
[self.view addSubview:blueView4];
[self.view addSubview:blueView5];
[blueView0 发布];
[blueView1 发布];
[blueView2 发布];
[blueView3 发布];
[blueView4 发布];
[blueView5 发布];
[self.view 发布];
} -(IBAction)viewClickedid)发件人{
{view/subview}.backgroundColor = [UIColor blackColor];
}
我是这样写委托的
@interface testAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
testViewController *viewController; //用于主视图
testViewController *viewController1;//for subview1
testViewController *viewController2;//for subview2
testViewController *viewController3;//for subview3
**最多 6 个控制器
}
@property(非原子,保留)IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet testViewController viewController;
@property(非原子,保留)IBOutlet testViewController viewController1;
**最多 6 个控制器
@结束
在 testAppdelegate.m 中,我正在为主视图制作一个控制器,但我不知道如何将其他控制器附加到其他子视图。现在当我触摸视图上的任何位置时,主视图颜色会发生变化。 我怎样才能唯一地识别不同的子视图触摸并使其颜色发生变化??如何做到这一点????
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
checkAppController *rootController = [checkAppController alloc];
[window addSubview:[rootController view]];
[window makeKeyAndVisible];
}
我在这个方法中得到了所有的触摸事件
- (void)touchesBeganNSSet *)touches withEventUIEvent *)event {
UITouch* touch = [触摸任何对象];
NSUInteger numTaps = [touch tapCount];
如果([触摸次数] > 1)
NSLog(@"mult-touches %d", [touches count]);
如果 (numTaps
} 否则 {
NSLog(@"双击");
}
}
【问题讨论】:
标签: iphone objective-c