添加子视图似乎没有任何作用
在我的应用程序中,当用户单击 infoButton 时,它应该在屏幕的特定位置添加另一个视图。我试图通过以下方法实现此行为:(
- (IBAction)showInfo1:(id)sender
{
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
[self.view addSubview:myView1];
}
我在类的头文件中声明了所有内容。)
当我运行代码并按下按钮时,似乎没有发生任何事情(我看不到新视图) 。
我还注意到 XCode 显示以下警告:
“myView1”的本地声明隐藏了实例变量。
有人有什么想法吗?
In my application, when the user clicks an infoButton, it should add another view to the screen at a specific location. I'm trying to achieve this behavior with the following method:
- (IBAction)showInfo1:(id)sender
{
UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];
[self.view addSubview:myView1];
}
(I declared everything in the header file of my class.)
When I run the code and press the button, nothing appears to happen (I don't see the new view).
I also noticed that XCode is displaying the following warning:
Local declaration of 'myView1' hides instance variable.
Does anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你怎么知道没有任何变化?
看起来 myView1 实际上不包含任何内容。尝试设置 myView1 的背景颜色
要在特定点打开它,您需要更改 CGRectMake() 中的参数,例如
在左上角打开它,宽度为 50,高度为 20,您将执行以下操作:
How do you know nothing changes?
It looks like myView1 doesn't actually contain anything. Try setting the background color of myView1
To open it at a specific point you need to change the parameters in CGRectMake() for example
to open it at the very top left with a width of 50 and height of 20 you would do:
“‘myView1’的本地声明隐藏了实例变量。”出现消息是因为您在类中声明了一些具有相同名称的属性(即使类型不同)。
如果您将
UIView *myView1
放入您的类定义中,您的方法将如下所示这里有更多注意事项:当您停止使用它时,必须释放
myView1
,避免被放置不止一次等等,但在这里我们有了基本的想法。最后,也许您的视图已经添加,但因为它还不包含任何内容,所以您没有注意到它。您还想检查 UIViewController 看看是否更适合您。
"Local declaration of 'myView1' hides instance variable." message appears because you have declared in your class some property with the same name (even if the type is different).
If you put
UIView *myView1
in your class definition, your method would look likeHere comes more considerations: you must to release
myView1
when you stop using it, avoid to be placed more than once, etc. but here we have the basic idea.Finally, maybe your view is already added, but 'cause it doesn't contains anything yet, you don't notice it. Also you would like to check UIViewController to see if works better for you.