AlertView的init方法中的参数

时间:2011-10-29 04:38:42

标签: iphone objective-c ios ios4 iphone-sdk-3.0

我想知道是否有一种方法可以在UIAlertView的构造函数中传递与initWithTitle中的参数不同的参数。特别是我想通过一个NSArray。可能吗? 这是代码:

@implementation UIAlertTableView


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {


    array=[NSArray arrayWithObjects:@"Capitaliz. semplice",@"Capitaliz. composta",@"Pagamenti rateali",@"Bond", nil];

    table = [[UITableView alloc] initWithFrame:CGRectZero  style:UITableViewStylePlain];

    table.delegate=self;
    table.dataSource=self;


    [self addSubview:table];
}
return self;
}

由于

3 个答案:

答案 0 :(得分:1)

每当你问自己这样的问题时,请搜索“[UIClassName]类引用”

UIAlertView类引用:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

它的init采用NSString,而不是字符串数组:

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate     cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

答案 1 :(得分:0)

我解决了,谢谢你:

#import "UIAlertTableView.h"



@implementation UIAlertTableView

@synthesize tableSelected,fontSize;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    c=0;    

    table = [[UITableView alloc] initWithFrame:CGRectZero  style:UITableViewStylePlain];

    table.delegate=self;
    table.dataSource=self;


    [self addSubview:table];
}
return self;
}

- (void)setFrame:(CGRect)rect {
if (c==0) 
    c++; 
else if(c==1){
    if([tableSelected isEqualToString:@"categorie"])

        array=[NSArray arrayWithObjects:@"Capitalizzazione semplice",@"Capitalizzazione composta",@"Pagamenti rateali",@"Bond",  nil];

    else if([tableSelected isEqualToString:@"tassi"])

        array=[NSArray arrayWithObjects:
               @"Tasso effettivo annuo",
               @"Tasso effettivo mensile",
               @"Tasso effettivo bimestrale",
               @"Tasso effettivo trimestrale",
               @"Tasso effettivo quadrimestrale",
               @"Tasso effettivo semestrale",
               @"Tasso nominale convertibile mensilmente",
               @"Tasso nominale convertibile bimestralmente",
               @"Tasso nominale convertibile trimestralmente",
               @"Tasso nominale convertibile quadrimestralmente",
               @"Tasso nominale convertibile semestralmente",
               nil];

    else if([tableSelected isEqualToString:@"rate"])


        array=[NSArray arrayWithObjects:
               @"Rata annuale",
               @"Rata mensile",
               @"Rata bimestrale",
               @"Rata trimestrale",  
               @"Rata quadrimestrale",  
               @"Rata semestrale",  
               nil];
    [table reloadData];
    [table selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
    c++;
}
[super setFrame:CGRectMake(0, 0, rect.size.width, 300)];
self.center = CGPointMake(320/2, 480/2);

}

答案 2 :(得分:0)

海报以另一种方式解决了他的问题。但是,一个简单的类别将起作用并且运作良好。它可以通过这种方式实现。

-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitlesArray:(NSArray *)otherButtonTitles{
    if ((self = [self initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil])){
        for (NSString *buttonTitle in otherButtonTitles) {
            [self addButtonWithTitle:buttonTitle];
        }
    }
    return self;
}

此方法采用最终参数。一个NSStrings数组,并迭代地添加带有字符串标题的按钮。