添加子视图似乎没有任何作用

发布于 2024-10-27 09:33:46 字数 430 浏览 4 评论 0原文

在我的应用程序中,当用户单击 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技术交流群

发布评论

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

评论(2

对你的占有欲 2024-11-03 09:33:47

你怎么知道没有任何变化?

看起来 myView1 实际上不包含任何内容。尝试设置 myView1 的背景颜色

- (IBAction) showInfo1: (id) sender
{
    UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];   
    myView1.backgroundColor = [UIColor redColor];
    [self.view addSubview: myView1]; 
}

要在特定点打开它,您需要更改 CGRectMake() 中的参数,例如

在左上角打开它,宽度为 50,高度为 20,您将执行以下操作:

CGRectMake(0, 0, 50, 20)

How do you know nothing changes?

It looks like myView1 doesn't actually contain anything. Try setting the background color of myView1

- (IBAction) showInfo1: (id) sender
{
    UIView *myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];   
    myView1.backgroundColor = [UIColor redColor];
    [self.view addSubview: 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:

CGRectMake(0, 0, 50, 20)
孤独陪着我 2024-11-03 09:33:47

“‘myView1’的本地声明隐藏了实例变量。”出现消息是因为您在类中声明了一些具有相同名称的属性(即使类型不同)。

如果您将 UIView *myView1 放入您的类定义中,您的方法将如下所示

- (IBAction)showInfo1:(id)sender{
  myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];   
  [self.view addSubview: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 like

- (IBAction)showInfo1:(id)sender{
  myView1 = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)];   
  [self.view addSubview:myView1]; 
}

Here 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.

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