【发布时间】:2011-05-26 12:00:17
【问题描述】:
使用 Erica Sadun 的滚动视图示例,我创建了一个项目。 我现在在我的项目中有一个滚动视图和一个页面控件。 我为每个页面创建一个新视图并将其添加到我的滚动视图中,如下所示。 每个视图都有 5 个按钮,所以我可以滚动 5 x 5。 但是按钮似乎没有启用。 任何想法为什么?
// Create the scroll view and set its content size and delegate
sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 200.0f, 300.0f, 60.0f)] autorelease];
sv.contentSize = CGSizeMake(NPAGES * 300.0f, sv.frame.size.height);
sv.pagingEnabled = YES;
sv.delegate = self;
// Load in all the pages
for (int i = 0; i < NPAGES; i++)
{
UIView *menuView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 60.0f)];
for (int j = 0; j < 5 * (i+1); j++)
{
UIImage *menuImage = [UIImage imageNamed:@"menuimage.png"];
UIImage *menuImage2 = [UIImage imageNamed:@"menuimageselected.png"];
UIButton *menuButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
menuButton.frame = CGRectMake(j * 60.0f, 0.0f, 60.0f, 60.0f);
[menuButton addTarget:self action:@selector(menuButtonClicked) forControlEvents:UIControlEventTouchUpInside];
menuButton.tag = j + 5 * i;
menuButton.backgroundColor = [UIColor clearColor];
[menuButton setImage:menuImage forState:UIControlStateNormal];
[menuButton setImage:menuImage2 forState:UIControlStateSelected];
[menuButton setImage:menuImage2 forState:UIControlStateHighlighted];
[menuButton setEnabled:YES];
[menuImage release];
[menuButton release];
[menuView addSubview:menuButton];
}
[sv addSubview:menuView];
[sv bringSubviewToFront:menuView];
[menuView release];
}
[self.view addSubview:sv];
【问题讨论】:
-
你怎么说他们没有启用?
-
这里,你还没有实现任何选择器,你怎么能说那个按钮没有启用呢?再次请详细说明您实际上想要做什么。
-
这段代码有很多错误 1.永远不要尝试保留按钮 2.我不认为你会在第二页上得到任何东西,因为你的 menuView.frame.origin.x 总是0 3. 你需要为你的按钮触摸事件添加目标
-
@7KV7:未启用是指当我触摸按钮时图像不会改变。
-
@rptwsthi:我现在实现了选择器。它仍然不起作用。但即使我没有实现选择器,当我触摸它时它应该仍然突出显示。采取的行动与它的外观无关。因为它是一个按钮,所以它应该已经具有在我触摸时突出显示的功能。对吗?
标签: iphone uiscrollview uibutton uipagecontrol