【问题标题】:When adding UIView instantiated with an .xib to the Storyboard buttons don't work and background color can't be changed将使用 .xib 实例化的 UIView 添加到 Storyboard 按钮不起作用并且背景颜色无法更改
【发布时间】:2015-04-11 07:33:36
【问题描述】:

我正在开发一个 iOS 项目,并希望包含一个 UIView,它可以在应用程序的多个屏幕上重复使用(出现在不同 UIView 的底部)。到目前为止,我正在为 UI 工作使用故事板,但创建了一个 .xib 文件供可重用视图使用(下面代码中的 playerView)。

视图被添加,但我添加到视图的按钮没有响应,而且我的背景颜色无法在视图上更改。我试图以编程方式在 .xib 中设置背景颜色,但没有成功。非常奇怪的症状,我试图从this article 的#5 实例化视图,并且可能做错了什么。我不完全理解文章中让我感到紧张的所有内容 - 例如,我的 showSubclassedView 方法返回 IBAction 但我只是在代码中调用函数名称并且不使用按钮(我确实按照文章所述连接了视图中的按钮) .

代码如下:

EventViewController.m(我尝试添加 PlayerView)

#import "EventViewController.h"
#import "PlayerView.h"

@interface EventViewController ()
-(IBAction)showSubclassedView;
@end

@implementation EventViewController


-(IBAction)showSubclassedView
{
    [PlayerView presentInViewController:self];
}

PlayerView.h(.h 用于可重用视图)

#import <UIKit/UIKit.h>
#import "Utils.h"

@class PlayerView;

@protocol PlayerViewDelegate
-(void)playerViewTouchedUp:(PlayerView*) playerView;
-(void)playerViewDidDismiss:(PlayerView*) playerView;
@end

@interface PlayerView : UIView

+(void)presentInViewController:(UIViewController<PlayerViewDelegate>*) playerView;
-(IBAction)viewTouchedUp;
-(IBAction)dismiss;

 @end

PlayerView.m(.m 用于可重复使用的视图)

#import "PlayerView.h"

@interface PlayerViewOwner : NSObject
@property(nonatomic,weak) IBOutlet PlayerView *playerView;
@end

@implementation PlayerViewOwner
@end

@interface PlayerView ()
@property (nonatomic, weak) UIViewController <PlayerViewDelegate> *delegateViewController;
@end

@implementation PlayerView

+(void)presentInViewController:(UIViewController<PlayerViewDelegate> *)viewController
{
    PlayerViewOwner *owner = [PlayerViewOwner new];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:owner options:nil];
    owner.playerView.delegateViewController = viewController;

    [viewController.view addSubview:owner.playerView];
}

-(IBAction)viewTouchedUp
{
    //forward to delegate
    NSLog(@"you clicked a button");
    [self.delegateViewController playerViewTouchedUp:self];
}

-(IBAction)dismiss
{
    [self removeFromSuperview];

    // Forward to delegate
    [self.delegateViewController playerViewDidDismiss:self];
}

@end

PlayerView.xib 上有一个 UIbutton 将其连接到 PLayerView.m 中的 viewTouchedUp 方法

我在上面的代码中做错了什么吗?这是在其他视图上显示可重用视图的最佳方式吗?

谢谢!

【问题讨论】:

  • 很棒的文章。作者提供了他们项目的 repo 链接,你检查了吗?但是,有几件事:确保为按钮启用了用户交互(假设您甚至没有看到 NSLog),显示您在代码中更改视图颜色的位置,并检查按钮的连接并制作确保您没有添加会干扰的额外连接。至于IBAction的使用,我相信它的处理方式与void相同,但会提示程序涉及界面构建器..但在这种情况下您仍然应该使用void
  • 我已将 IBAction 更改为 void,这样做很好,但你说得对,它没有改变任何东西。我没有看到任何其他不合适的连接,但我将通过作者在 Xcode 中发布的代码来查看。我没有以编程方式更改背景颜色,而是在 PlayerView.xib 中更改它,我应该提到这一点。是的,尽管选中了启用用户交互的复选框,但 NSLog 甚至没有命中。今晚我会去作者的 github repo 看看,如果我发现了什么,请报告。
  • 我查看了作者的 github 仓库,也无法更改他们的背景!我发现了一种不同的方法,它仍然有很好的分离效果(不是很好),但至少可以工作。

标签: ios objective-c iphone xcode uiview


【解决方案1】:

这是我在无法解决此问题后找到的解决方法。

New implementation is from this article

我从最初的问题中恢复了我的更改并实施了这个。它缺乏用于来回交谈的良好协议分离,我以后可能需要这样的东西,但我认为现在我仍然可以实现一个协议来解决这个问题。

至少有了这个解决方案,我的视图中有可用的项目和变化的背景!

【讨论】:

    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 2014-09-25
    • 2022-11-21
    • 2020-01-01
    • 2017-05-30
    • 2014-04-09
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多