2011-10-29 55 views
0

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

@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; 
} 

感谢

+0

我没有看到你的调用UIAlertView在该代码中 - 只有数组的创建... – bryanmac

+0

你想传递的警报视图中的按摩字符串(这就是我们所做的),是的,你可以通过那很容易。 – rptwsthi

+0

你期待警报视图如何处理这个数组? – jrturton

回答

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, ... 
+0

你可以重写方法initWithTitle ?! –

+0

您可以使用自己的方法继承和扩展它,也可以使用类别进行扩展。你不能重写,因为这意味着具有重写实现的相同签名 – bryanmac

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); 

} 
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并迭代地添加带有字符串标题的按钮。