制作自定义 UISegmentControl(某种程度)

发布于 2024-10-29 12:00:37 字数 390 浏览 1 评论 0原文

我有这个模型:

在此处输入图像描述

正如您所看到的,这是一种导航菜单。它的功能应该与分段控件相同,我将根据活动项目更改 tableView。

实现这个最简单的方法是什么?

我已经开始创建我的 UIView 子类,但发现我必须创建一个委托,监视点击事件和其他内容。 这是最好的方法吗? 我应该子类化 UISegmentedControl 吗?

还有其他建议吗?

请为我指出正确的方向。我对 Obj-c 充满信心,但是制作这些东西让我的想法变得疯狂。

I've got this mock up:

enter image description here

As you can see, it's a sort of navigation-menu. It's functionality should be the same as a segmented control, and i am going to change the tableView based on the item active.

What would be the easiest way to implement this?

I have started makin my UIView-subclass, but found out that i had to then make a delegate, watch for tap-events and stuff.
Is this the best way to do it? Should i subclass UISegmentedControl?

Any other advice?

Please, point me in the right direction. I feel confident in Obj-c, but making these kinds of stuff makes my mind goes crazy.

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

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

发布评论

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

评论(1

在巴黎塔顶看东京樱花 2024-11-05 12:00:37

从概念上讲,UISegmentedControl 似乎是一个不错的选择,但我认为它不够灵活,无法创建您想要的效果。

您是否考虑过将三个 UIButton 控件放入自定义视图中?您可以使用 setBackgroundImage:forState: 自定义每个按钮的图像,以获取模型中的边框样式。将按下的按钮的 selected 属性设置为 YESUIButton 将为您处理绘图。

您可以设置一个操作方法来通过调用来检测按下了哪个按钮。

[button addTarget:self action:@selector(nameOfMethodToHandleButtonPress) forControlEvents:UIControlEventTouchUpInside])]

委托是符合您创建的协议的任何类。因此,您可以在标头中创建一个委托协议,如下所示:

@class MyControl; // this is a forward reference to your class, as this must come before the class @interface definition in the header file

@protocol MyControlDelegate <NSObject>

@optional
- (void)myControl:(MyControl *)control didSelectButton:(int)buttonIndex; // replace this method with whatever makes sense for your control

@end

委托只是您的 MyControl 类中的一个属性:

@property (nonatomic, assign) id <MyControlDelegate> delegate; // you use 'assign' instead of 'retain' to prevent retain cycles

在您的按钮按下处理程序中,例如:

- (void)methodThatHandlesButtonPress { // this is the method you set up in the first code example with addTarget:action:forCotnrolEvents:
    if ([self.delegate respondsToSelector:@selector(myControl:didSelectButton:)])
        [self.delegate myControl:self didSelectButton:0]; // replace as appropriate
}

现在,您只需拥有包含您的控件的视图控制器采用协议:

@interface MyViewController : UIViewController <MyControlDelegate> { // etc...

并实现方法:

- (void)myControl:(MyControl *)control didSelectButton:(int)buttonIndex {
    // handle the button press as appropriate
}

Conceptually, UISegmentedControl seems like a good choice for this, but I don't think it's quite flexible enough to create the effect you're going for here.

Have you considered putting three UIButton controls inside a custom view? You can customize the images for each button using setBackgroundImage:forState: to get the border style in your mockup. Set the selected property of the pressed button to YES, and UIButton will handle the drawing for you.

You can set up an action method to detect which button was pressed by calling

[button addTarget:self action:@selector(nameOfMethodToHandleButtonPress) forControlEvents:UIControlEventTouchUpInside])]

A delegate is just any class that conforms to a protocol you create. So you would create a delegate protocol in your header like this:

@class MyControl; // this is a forward reference to your class, as this must come before the class @interface definition in the header file

@protocol MyControlDelegate <NSObject>

@optional
- (void)myControl:(MyControl *)control didSelectButton:(int)buttonIndex; // replace this method with whatever makes sense for your control

@end

And the delegate is just a property in your MyControl class:

@property (nonatomic, assign) id <MyControlDelegate> delegate; // you use 'assign' instead of 'retain' to prevent retain cycles

And in your button press handlers, for example:

- (void)methodThatHandlesButtonPress { // this is the method you set up in the first code example with addTarget:action:forCotnrolEvents:
    if ([self.delegate respondsToSelector:@selector(myControl:didSelectButton:)])
        [self.delegate myControl:self didSelectButton:0]; // replace as appropriate
}

Now, you just have to have the view controller that contains your control adopt the protocol:

@interface MyViewController : UIViewController <MyControlDelegate> { // etc...

And implement the method:

- (void)myControl:(MyControl *)control didSelectButton:(int)buttonIndex {
    // handle the button press as appropriate
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文