Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过布局子视图自定义UIAlertButton=>;找不到UIAlertButton类_Ios_Objective C_Xcode_Ipad_Uialertview - Fatal编程技术网

Ios 通过布局子视图自定义UIAlertButton=>;找不到UIAlertButton类

Ios 通过布局子视图自定义UIAlertButton=>;找不到UIAlertButton类,ios,objective-c,xcode,ipad,uialertview,Ios,Objective C,Xcode,Ipad,Uialertview,我正在尝试使用此工具实现自定义警报视图。所有这些都可以很好地工作,但它并没有提到定制UIAlertButton。 然后,我尝试通过layoutSubviews方法对其进行自定义: - (void)layoutSubviews { for (UIView *subview in self.subviews){ //Fast Enumeration if ([subview isMemberOfClass:[UIImageView class]]) {

我正在尝试使用此工具实现自定义警报视图。所有这些都可以很好地工作,但它并没有提到定制UIAlertButton。 然后,我尝试通过layoutSubviews方法对其进行自定义:

- (void)layoutSubviews
{
    for (UIView *subview in self.subviews){ //Fast Enumeration
        if ([subview isMemberOfClass:[UIImageView class]]) {
            subview.hidden = YES; //Hide UIImageView Containing Blue Background
        }
        if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
            UILabel *label = (UILabel*)subview; //Cast From UIView to UILabel
            label.textColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f];
            label.shadowColor = [UIColor blackColor];
            label.shadowOffset = CGSizeMake(0.0f, 1.0f);
        }
        if ([subview isMemberOfClass:[UIAlertButton class]]) {
            // do blablabla
        }
    }
}
特别是我补充的是:

if ([subview isMemberOfClass:[UIAlertButton class]]) {
     // do blablabla
}
但它找不到UIAlertButton类,为什么? 谢谢

编辑: 这是我最后的工作代码,感谢nsguliver

CustomAlertView.h

#import <UIKit/UIKit.h>

@interface CustomAlertView: UIAlertView {
    NSMutableArray *fakeButtonIndexList;
    NSMutableArray *buttonList;
}

-(void)addCustomButton:(NSString*)title;

@end

有一种方法可以自定义
UIAlertView
上的按钮,首先将两个按钮标题保持为
nil
,然后将自定义按钮作为
子视图添加到
CustomAlertView
中,请参见下面的代码

CustomAlertVIew *alert =[[CustomAlertVIew alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

// add the custom button
UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

//create the button and assign the image
UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.alertButton setFrame:CGRectMake(x,y,buttonImage.size.width,buttonImage.size.height)];

[alertButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
alertButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[alertButton setTitle:@"Done" forState:UIControlStateNormal];
[alertButton addTarget:self action:@selector(backMethod)  forControlEvents:UIControlEventTouchUpInside];

[alert addSubview:alertButton];
[alert show];

你的问题已经得到了很好的回答,但我的解决方案可能更短更简洁。 首先,我创建了一个变量,用于跟踪子视图数组中的按钮索引。我假设AlertViewButtons是最后一个子视图:

int buttonIndexInSubviews = (self.subviews.count - self.numberOfButtons);
然后,我遍历了如图所示的子视图,而if语句的格式不同。诀窍是将AlertViewButton强制转换为超类

if (subview == [self.subviews objectAtIndex:buttonIndexInSubviews]) {
        UIButton *button = (UIButton*)subview;

        //Here you can change the button appearance via the variable
        //E.g. button.titleLabel.shadowOffset = CGSizeMake(0.0f, 0.0f);

        buttonIndexInSubviews++;
    }

顺便说一句,此解决方案已获得Apple的批准。

或者您可以对UIAlertButton类进行如下测试:

if ([subview isMemberOfClass:NSClassFromString(@"UIAlertButton")]) {
    // do your customizing here...
}

您是否将自定义
ui按钮
用作
ui警报按钮
?为什么需要
UIAlertButton
?当我登录subview.class时,它会返回UIAlertButton(UIAlertView中的默认按钮),为什么我不能访问我应用程序中的任意位置的UIAlertButton类?嗯,好的,坏的:/谢谢!不,没有参考资料,即使是课堂参考资料。。。无论如何,它存在于某个地方,因为当我记录类时,它说“UIAlertButton”。无论如何,我会尝试通过initWithTitle方法以外的其他方法添加按钮。是的,我用工作解决方案升级了我的帖子,谢谢你!现在,我将尝试在警报视图中放置自定义文本字段。
if ([subview isMemberOfClass:NSClassFromString(@"UIAlertButton")]) {
    // do your customizing here...
}