ios点击按钮改变背景

发布于 2024-11-29 09:03:33 字数 242 浏览 8 评论 0原文

到目前为止,我只有界面构建器布局

我不清楚从布局中引用所有这些项目的语法

我知道必须在某个地方使用 IBOutlet ,但我需要更多的掌握关于这个目标 C 正在做什么。我读过的任何内容都没有告诉我为什么有些声明以 + 开头,而另一些声明以 - 开头。

我想要做的是单击布局中的按钮,弹出模式视图并更改整个布局的背景。

所以第一步是参考我在笔尖中制作的所有这些项目。帮助? (或发布您了解的更直观教程的链接)

I so far only have the interface builder layout

I'm not clear on the syntax to reference all of these items from the layout

I know that IBOutlet has to be used somewhere, but I need a bit more handholding on what this objective C is doing. Nothing I've read tells me exactly why some declarations start with + and others with -

What I want to do is click a button in my layout, have a modal view pop up and change the background on the entire layout.

so the first step is referencing all these items I've made in the nib. help? (or post a link to more intuitive tutorials that you know about)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

上课铃就是安魂曲 2024-12-06 09:03:33

所以您可能想为您的背景视图创建一个 IBOutlet。也许它是一个 UIImageView,您可以根据用户在模式视图中选择的内容来设置它的图像属性。为此,您只需在 IB 文件中声明 UIImageView

UIImageView *imageView;

,然后将其声明为属性

@property (nonatomic,retain)IBOutlet UIImageView *imageView;

并在 .m 文件中合成它

@synthesize imageView;

。如果您不使用 ARC,请不要忘记释放它。

然后您可以打开界面生成器,如果您单击视图控制器文件所有者并转到连接检查器,您将看到那里有一个用于 imageView 的新连接。只需将该连接拖到 IB 文件中的 UIImageView 即可。现在,您的代码中有一个连接到 IB 中的 UIImageView 的引用。

这将允许您通过键入类似的内容来在代码中设置 UIImageView

self.imageView.image = [UIImage imageNamed:theNameTheUserJustPicked];

。 为了获得模态视图,您需要 IBAction 来触发代码中的方法,因此在主笔尖的 .h 文件中声明这样的方法。

- (IBAction)displayViewBackgroundChooser;

然后在您的 .m 文件中定义它。

- (IBAction)displayViewBackgroundChooser {
    //present your new view on screen here
}

然后返回界面生成器并再次单击文件所有者。您应该在连接检查器中看到它,然后您可以将其连接到一个按钮,例如,这将触发该方法。

希望这有助于澄清 IBOutlet 和 IBActions 上的问题。

So you probably want to create an IBOutlet for your background view. Maybe it's a UIImageView that you can set it's image property based on what the user selects in the modal view. For this you would just declare the UIImageView you have in your IB file

UIImageView *imageView;

and then declare it as a property

@property (nonatomic,retain)IBOutlet UIImageView *imageView;

and synthesize it in your .m file

@synthesize imageView;

Don't forget to release it if you're not using ARC.

Then you can open up interface builder and if you click on your view controller File's Owner and go to the connections inspector you will see there is a new connection there for imageView. Just drag that connection over to your UIImageView in the IB file and that's it. You now have a reference in your code that connects to your UIImageView in IB.

That will allow you to set the UIImageView in your code by typing something like

self.imageView.image = [UIImage imageNamed:theNameTheUserJustPicked];

In order to get the modal view, you need an IBAction to trigger a method in your code so declare one like this in your .h file of your main nib.

- (IBAction)displayViewBackgroundChooser;

and then define it in your .m file.

- (IBAction)displayViewBackgroundChooser {
    //present your new view on screen here
}

Then go back to interface builder and click on the File's Owner again. You should see it there in the connections inspector and then you can connect it to a button, for example, that would trigger that method.

Hope this helps to clear things up a bit on IBOutlets and IBActions.

淡写薰衣草的香 2024-12-06 09:03:33

您可以通过 IBOutletsIBActions 使在 IB 中创建的 UI 元素与您的代码进行交互。

在您的情况下,我会将一个操作与按钮关联起来,以便在单击按钮时触发该操作;该操作将打开一个模态视图,您可以在关联控制器的 viewDidLoad 方法中更改该视图的背景。

在这里您可以找到有关添加出路。和

关于你对+和-的疑问,-标识了类中定义的普通方法; + 定义一个类方法,即可以直接调用该类的方法,而无需先实例化它。请参阅这篇SO文章了解更多信息。

You can make your UI elements created in IB interact with your code by means of IBOutlets and IBActions.

In your case, I would associate an action to the button, so that it is fired when the button is clicked; the action would open a modal view, and you could change the background of that view in the viewDidLoad method of the associated controller.

Here you find a video tutorial about adding an outlet. And here, the same about actions.

About your doubt on + and -, - identifies a normal method defined in a class; + defines a class method, i.e., a method that you can call on the class directly, without having to instantiate it first. Have a look at this S.O. article for more.

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