2011-10-29 46 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並迭代地添加帶有字符串標題的按鈕。