【问题标题】:Parameters in the init method of a AlertViewAlertView 的 init 方法中的参数
【发布时间】:2011-10-29 04:38:41
【问题描述】:

我想知道是否有一种方法可以在不同于 initWithTitle 的 UIAlertView 的构造函数中传递参数。特别是我想传递一个 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;
}

谢谢

【问题讨论】:

  • 我没有在该代码中看到您对 UIAlertView 的调用 - 只有数组的创建...
  • 你想在警报视图中传递一个按摩字符串(这就是我们所做的一切),是的,你可以轻松传递。
  • 你希望警报视图如何处理这个数组?
  • 你最终找到答案了吗?

标签: iphone objective-c ios ios4 iphone-sdk-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, ...

【讨论】:

  • 你可以重写方法initWithTitle?!
  • 您可以使用自己的方法继承和扩展它,也可以使用类别进行扩展。您不能覆盖,因为这意味着具有覆盖实现的相同签名
【解决方案2】:

我解决了,还是谢谢:

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

}

【讨论】:

    【解决方案3】:

    发帖人用另一种方式解决了他的问题。但是,是的,一个简单的类别将有效并且运作良好。可以这样实现。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多