iphone - 将 subview 添加到自定义类的 super View 不起作用

标签 iphone ios objective-c xcode cocoa-touch

<分区>

我正在尝试学习如何创建一个自定义类,该类可以将 subview 添加到它的 super View 中,并相信我下面的代码应该可以工作,但事实并非如此,而且我对原因的理解还不够。它成功构建并通过添加 subview 运行,但我从未在我的模拟器上看到它。我希望有人能指出我正确的方向。

mainviewcontroller.m 导入#alerts.h 并尝试运行

Alerts* al = [[Alerts alloc] initWithFrame:[self.view bounds]];

[al throwBottomAlert:@"message" withTitle:@"Title Test"];

在我的自定义类中...

头文件

#import <UIKit/UIKit.h>

@interface Alerts : UIAlertView

- (void)throwBottomAlert:(NSString*)message withTitle:(NSString*)title;


@end

执行文件

#import "Alerts.h"

@implementation Alerts

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)throwBottomAlert:(NSString*)message withTitle:(NSString*)title {

    UIView* alertView = [[UIView alloc] initWithFrame:[self bounds]];
    alertView.backgroundColor = [UIColor blackColor];

    [self.superview addSubview:alertView];
    [self.superview bringSubviewToFront:alertView];

} 

最佳答案

这里有几个问题。我会先从最糟糕的新开始。不支持子类化 UIAlertView,这不是一个好主意。

Subclassing Notes

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

UIAlertView Class Reference

下一条坏消息,-initWithFrame: 不是UIAlertView 的指定初始化程序,不应使用。您需要使用 -initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

最后,现有 UIAlertView 的 super View 是 _UIAlertNormalizingOverlayWindow_UIAlertNormalizingOverlayWindowUIWindow 的子类型,没有 super View 。这意味着您看到的警报不存在于您所有应用程序查看的同一窗口中。

关于iphone - 将 subview 添加到自定义类的 super View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304808/

上一篇:ios - html src 正则表达式 iOS

下一篇:ios - 如何连接 TextView.text

相关文章:

iphone - UITableViewCell 中复选标记的问题

iphone - 为未推送的 View 设置动画

iphone - 获取iPhone/iPad屏幕上显示的文字

objective-c - 从逗号分隔文件加载类的最佳方法

iphone - 按颜色分割 UIImage 并创建 2 个图像

iphone - Facebook SSO 未登录(isSessionValid 始终返回 NO)

iphone - Cocoa Pods 在使用框架时出现重复符号错误

ios - 从 UICollectionViewCell 呈现 AlertView

ios - 如何隐藏具有纵横比自动布局约束的 UIImageView?

ios - 即使在将相同的线程标识符设置为 UNMutableNotificationContent 后,UNUserNotificationCenter 也不会将通知组合在一起