如何获取UIBarButtonItem的嵌入按钮

发布于 2024-09-05 15:32:58 字数 316 浏览 9 评论 0原文

在iPhone应用程序中,我们可以使用以下代码制作一个UIBarButtonItem:

UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

生成的UIBarButtonItem具有系统提供的“操作”图标。我的问题是:如何获取此UIBarButtonItem的内部UIButton并将此UIButton添加到另一个UIView?谁可以给我看一些代码?

In an iPhone app,we can make a UIBarButtonItem by using the following code:

UIBarButtonItem *bbix=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

the generated UIBarButtonItem has a system-provided "action" icon.My question is :how to get this UIBarButtonItem's inner UIButton and add this UIButton to another UIView? who can show me some code?

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

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

发布评论

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

评论(7

谁的年少不轻狂 2024-09-12 15:32:59

你不能。 UIBarButtonItem 继承自 UIBarItem,而 UIBarItem 又继承于 NSObject。这些类都没有获取 UIButton 的方法,因为 UIBarButtonItem 不是 UIButton

相反,您可以从 UIButton 创建 UIBarButtonItem - 请参阅我对另一个问题的回答以获取更多信息(通过将其添加为“自定义视图”来工作)。

You can't. UIBarButtonItem inherits from UIBarItem which inherits from NSObject. None of those classes has a method to get a UIButton, because UIBarButtonItem isn't a UIButton.

Contrariwise, you can create a UIBarButtonItem from a UIButton -- see an answer I gave to a different question for more info (which works by adding it as a "custom view").

你对谁都笑 2024-09-12 15:32:59

访问栏按钮项的“customView”属性是一种可能的解决方案:

UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItems objectAtIndex:0];
                     OR
UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItem];

UIButton *myBtn;

if([item.customView isKindOfClass:[UIButton class]]) 
{ 
    myBtn = (UIButton*)item.customView;
}

if(myBtn)
{
    // do something
}

尝试一下。它真的对我有用:)

Accessing the "customView" property of the bar button item is one possible solution:

UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItems objectAtIndex:0];
                     OR
UIBarButtonItem *item = (UIBarButtonItem *)[self.navigationItem.rightBarButtonItem];

UIButton *myBtn;

if([item.customView isKindOfClass:[UIButton class]]) 
{ 
    myBtn = (UIButton*)item.customView;
}

if(myBtn)
{
    // do something
}

Try this out. It really works for me :)

橘和柠 2024-09-12 15:32:59

我认为无论如何你都无法获得嵌入式按钮。但是,如果您使用自定义视图创建按钮,例如使用按钮,我们可以稍后使用以下代码获取该按钮:

((UIButton *)(theBarButtonItem.customView.subviews.lastObject));

自定义视图的层次结构应该像这样:

|--UIView
  |--UIButton
  |--UIImageView

希望有所帮助。

I don't think you can get the embedded button in any case. However, if you create a button using a custom view, for example, using a button, we can get that button later using this code:

((UIButton *)(theBarButtonItem.customView.subviews.lastObject));

The hierarchy of the custom view should like this:

|--UIView
  |--UIButton
  |--UIImageView

Hopes that helps.

浅笑依然 2024-09-12 15:32:59

您无法通过 UIBarButtonItem 访问 UIBarButtonItem 内的嵌入按钮,

您可以使用界面生成器与新的插座链接内部按钮,这样您就可以直接访问该按钮
当您打开 nib 文件时,您可以在对象视图中找到该按钮,然后您可以将其与新的插座链接

You can't access the embedded button inside UIBarButtonItem through the UIBarButtonItem

what you can do it to link the inner button using the interface builder with a new outlet and like this you can access to the button directly
you can find the button inside the objects view when you open the nib file then you can link it with a new outlet

半寸时光 2024-09-12 15:32:59

只是为了扩展 Brians 的答案,如果您的 UIBarButton 是使用自定义视图制作的...

if ([myBarButton.customView.subviews.lastObject isKindOfClass:[UIButton class]])
{
    UIButton * btn = ((UIButton *)(myBarButton.customView.subviews.lastObject));
}

只需添加一点错误检查,您永远不知道苹果或其他开发人员有一天会更改布局层次结构。因为从长远来看,这种方法确实很容易出错。

Just to extend Brians answer if your UIBarButton is made with custom view...

if ([myBarButton.customView.subviews.lastObject isKindOfClass:[UIButton class]])
{
    UIButton * btn = ((UIButton *)(myBarButton.customView.subviews.lastObject));
}

Just add a little bit of error checking, you never know when apple or another developer will one day change the layout hierarchy. Since this approach is really error prone long term.

谷夏 2024-09-12 15:32:59

如果您通过 initWithCustomView 创建了栏按钮项目,只需访问自定义视图属性并转换为 UIButton。

http://cocoatouch.blog138.fc2.com/blog-entry-214.html

If you have created your bar button item via initWithCustomView, simply access the custom view property and cast to UIButton.

http://cocoatouch.blog138.fc2.com/blog-entry-214.html

梦回梦里 2024-09-12 15:32:59

这是一个精确复制 UIBarButtonItem 图像的解决方案,该图像是通过使用 UIBarButtonSystemItemAction 系统类型获取的。例如,新创建的 UIButton 被插入到 MKAnnotationView 中:

创建一个包含此方法的类别文件:

@implementation UIImage (Custom)

+ (UIImage *)actionButtonImage
{
    CGRect rect = CGRectMake(0, 0, 20, 27);

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

    [[UIColor colorWithRed:3/255.0 green:122/255.0 blue:1 alpha:1] set];

    UIBezierPath *path = [UIBezierPath bezierPath];

    // Box
    [path moveToPoint:CGPointMake(7, 8)];
    [path addLineToPoint:CGPointMake(1, 8)];
    [path addLineToPoint:CGPointMake(1, 26)];
    [path addLineToPoint:CGPointMake(19, 26)];
    [path addLineToPoint:CGPointMake(19, 8)];
    [path addLineToPoint:CGPointMake(13, 8)];

    // Arrow shaft
    [path moveToPoint:CGPointMake(10, 17)];
    [path addLineToPoint:CGPointMake(10, 1)];

    // Arrow head
    [path moveToPoint:CGPointMake(6, 4.5)];
    [path addLineToPoint:CGPointMake(10, 0.5)];
    [path addLineToPoint:CGPointMake(14, 4.5)];

    [path stroke];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end

MKMapView 委托中,添加此实现(根据需要进行调整) :

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Item"];
    view.canShowCallout = YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *actionImage = [UIImage actionButtonImage];
    [button setImage:actionImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, actionImage.size.width, actionImage.size.height);
    view.leftCalloutAccessoryView = button;

    return view;
}

Here is a solution that precisely replicates the UIBarButtonItem image that is otherwise acquired by using the UIBarButtonSystemItemAction system type. As an example, the newly created UIButton is inserted into an MKAnnotationView:

Create a category file that contains this method:

@implementation UIImage (Custom)

+ (UIImage *)actionButtonImage
{
    CGRect rect = CGRectMake(0, 0, 20, 27);

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);

    [[UIColor colorWithRed:3/255.0 green:122/255.0 blue:1 alpha:1] set];

    UIBezierPath *path = [UIBezierPath bezierPath];

    // Box
    [path moveToPoint:CGPointMake(7, 8)];
    [path addLineToPoint:CGPointMake(1, 8)];
    [path addLineToPoint:CGPointMake(1, 26)];
    [path addLineToPoint:CGPointMake(19, 26)];
    [path addLineToPoint:CGPointMake(19, 8)];
    [path addLineToPoint:CGPointMake(13, 8)];

    // Arrow shaft
    [path moveToPoint:CGPointMake(10, 17)];
    [path addLineToPoint:CGPointMake(10, 1)];

    // Arrow head
    [path moveToPoint:CGPointMake(6, 4.5)];
    [path addLineToPoint:CGPointMake(10, 0.5)];
    [path addLineToPoint:CGPointMake(14, 4.5)];

    [path stroke];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@end

In the MKMapView delegate, add this implementation (adapt as desired):

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Item"];
    view.canShowCallout = YES;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *actionImage = [UIImage actionButtonImage];
    [button setImage:actionImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, actionImage.size.width, actionImage.size.height);
    view.leftCalloutAccessoryView = button;

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