2010-11-22 165 views
0

我很努力地解決了這個問題。我有一個自定義設計的UIAlertView(一個新的UIAlertView繼承類,其名稱是ProductAlertView),我想打開一個新的自定義設計的UIAlertView(也是一個UIAlertView繼承類)。當我在自定義設計的alertview中創建這個新的自定義設計的UIAlertView時,新設計的alertview看起來不太好(有些圖像丟失了......) 另外,我試圖實現一個singleton類,它關閉第一個alertview,然後打開第二個alertview ;然而,這並沒有改變,認爲這是不完整的,並再次看到。在定製設計的UIAlertView中調用自定義設計的UIAlertView

有沒有人有解決方案?

一些代碼:

首先AlertView

#import "ProductAlertView.h" 
#import <QuartzCore/QuartzCore.h> 
#import "cocos2d.h" 
#import "MessageAlertView.h" 
#import "Communicator.h" 
#import "MainGameScene.h" 
#import "UpdateMe.h" 

@implementation ProductAlertView 
@synthesize image; 
@synthesize unitId; 
@synthesize player_id; 
@synthesize location_id; 
@synthesize unitName; 
@synthesize currentLevel; 
@synthesize tab1Name; 
@synthesize alertName; 
@synthesize imageTab1; 
@synthesize buttonTab1; 
@synthesize scrollView; 
@synthesize productArray; 
@synthesize seller_id; 

//int MAXLEVEL = 5; 


-(void) drawRect:(CGRect)rect{ 
    CGSize size = self.image.size; 
    [self.image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
} 



- (void) show { 
    // Test the labels 


    // call the super show method to initiate the animation 
    [super show]; 

    // resize the alert view to fit the image 
    CGSize imageSize = self.image.size; 
    self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height); 



    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(6, 40, 440, 180)]; 
    [scrollView setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.20]]; 
    scrollView.layer.cornerRadius = 10; 
    //scrollView.alpha = 1.0; 
    int i; 
    int j = 2; 
    for (i = 0; i < [productArray count]; i++) { 
     NSDictionary *unitDict = [productArray objectAtIndex:i]; 
     UIImage *image2 = [UIImage imageNamed:@"bina_s_1.png"]; 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 107, 154)]; 
     imageView.image = image2; 

     UILabel *moneyLbl = [[UILabel alloc] initWithFrame:CGRectMake(55, 80, 40, 20)]; 
     NSString *moneyStr = [[unitDict objectForKey:@"buyPrice"] description]; 
     [moneyLbl setText:moneyStr]; 
     [moneyLbl setFont:[UIFont fontWithName:@"Comic Sans MS" size:15]]; 
     [moneyLbl setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.0]]; 

     UILabel *minLbl = [[UILabel alloc] initWithFrame:CGRectMake(55, 100, 40, 20)]; 
     NSString *durationStr = [[unitDict objectForKey:@"name"] description]; 
     [minLbl setText:durationStr]; 
     [minLbl setFont:[UIFont fontWithName:@"Comic Sans MS" size:15]]; 
     [minLbl setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.0]]; 

     UILabel *dpLbl = [[UILabel alloc] initWithFrame:CGRectMake(55, 120, 40, 20)]; 
     NSString *xpStr = [[unitDict objectForKey:@"xp"] description]; 
     [dpLbl setText:xpStr]; 
     [dpLbl setFont:[UIFont fontWithName:@"Comic Sans MS" size:15]]; 
     [dpLbl setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.0]]; 

     [imageView addSubview:moneyLbl]; 
     [imageView addSubview:minLbl]; 
     [imageView addSubview:dpLbl]; 

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(j, 5, 107, 154); 
     button.tag = i; 
     [button addSubview:imageView]; 
     [button addTarget:self action:@selector(clickType:) forControlEvents:UIControlEventTouchUpInside]; 
     [scrollView addSubview:button]; 
     j += 110; 
    } 

    UILabel *tab1Lbl = [[UILabel alloc] initWithFrame:CGRectMake(19, 7, 70, 20)]; 
    [tab1Lbl setText:tab1Name]; 
    [tab1Lbl setFont:[UIFont fontWithName:@"ComicSansMS" size:15]]; 
    [tab1Lbl setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.0]]; 

    UILabel *alertLbl = [[UILabel alloc] initWithFrame:CGRectMake(340, 5, 85, 20)]; 
    [alertLbl setText:alertName]; 
    [alertLbl setFont:[UIFont fontWithName:@"ComicSansMS" size:15]]; 
    [alertLbl setBackgroundColor:[UIColor colorWithRed:0.92 green:0.59 blue:0.062 alpha:0.0]]; 

    UIImage *image4 = [UIImage imageNamed:@"exit1.png"]; 
    UIButton *buttonExit = [UIButton buttonWithType:UIButtonTypeCustom]; 
    buttonExit.frame = CGRectMake(425, 5, 21, 20); 
    [buttonExit setImage:image4 forState:UIControlStateNormal]; 
    [buttonExit addTarget:self action:@selector(exitAlert) forControlEvents:UIControlEventTouchUpInside]; 

    imageTab1 = [UIImage imageNamed:@"panel_btn_active.png"]; 
    buttonTab1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
    buttonTab1.frame = CGRectMake(3, 7, 93, 20); 
    [buttonTab1 setImage:imageTab1 forState:UIControlStateNormal]; 

    [scrollView setScrollEnabled:YES]; 
    CGSize size1; 
    size1.width = 550; 
    size1.height = 75; 
    [scrollView setContentSize:size1]; 


    [self addSubview:buttonTab1]; 
    [self addSubview:scrollView]; 
    [self addSubview:tab1Lbl]; 
    [self addSubview:alertLbl]; 
    [self addSubview:buttonExit]; 
} 
- (void) clickType:(id) sender{ 

    int myTag = [sender tag]; 
    NSLog(@"Sender Tag:%i",myTag); 
    //[self dismissWithClickedButtonIndex:0 animated:YES]; 
    /* 
    MessageAlertView *alertView = [[MessageAlertView alloc] initWithFrame:self.frame]; 
    [alertView setImage:[UIImage imageNamed:@"tebrikler_panel.png"]]; 

    [alertView show]; 
    */ 
    NSString *str; 
    NSDictionary *collectibleDict = [productArray objectAtIndex:myTag]; 

    Communicator *comm = [[Communicator alloc] init]; 
    [comm buyAndSellCollectible:[location_id intValue] withBuyerId:[player_id intValue] withSellerId:[seller_id intValue] withCollectibleId:[[collectibleDict objectForKey:@"collectible_id"] intValue]]; 
    NSString *respStr = [NSString stringWithUTF8String:[[comm getRespStr] cStringUsingEncoding:NSUTF8StringEncoding]]; 
    NSLog(@"%@",respStr); 
    NSDictionary *dict = [respStr JSONValue]; 

    NSString *header; 

    if (![[dict objectForKey:@"msgtype"] isEqualToString:@"error"]) { 
     header = [NSString stringWithString:@"Helallll!"]; 
     [UpdateMe checkLevel:[player_id intValue]]; 
     [UpdateMe checkMoneyXP:[player_id intValue]]; 
     [UpdateMe checkBagStatus:[player_id intValue]]; 
    } 
    else{ 
     header = [NSString stringWithString:@"Errorrrr!"]; 
    } 

    str = [NSString stringWithFormat:@"%@",[dict objectForKey:@"msg"]]; 

    [[MainGameScene sharedMySingleton] showMessageAlertView:@"Hello" withMessage2:@"Hello 2" closeAlertView:self]; 
} 
- (void) exitAlert{ 
    [self dismissWithClickedButtonIndex:0 animated:YES]; 
} 
- (void) dealloc{ 
    [unitId release]; 
    [player_id release]; 
    [location_id release]; 
    [seller_id release]; 
    [unitName release]; 
    [currentLevel release]; 
    [productArray release]; 
    [tab1Name release]; 
    [alertName release]; 
    [imageTab1 release]; 
    [buttonTab1 release]; 
    [scrollView release]; 
    [image release]; 
    [super dealloc]; 
} 

@end 

二AlertView

#import "MessageAlertView.h" 


@implementation MessageAlertView 

@synthesize image; 
@synthesize message1; 
@synthesize message2; 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 


- (void)drawRect:(CGRect)rect { 
    CGSize size = self.image.size; 
    [self.image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
} 

- (void) show{ 

    [super show]; 
    CGSize imageSize = self.image.size; 
    self.bounds = CGRectMake(0,0,imageSize.width,imageSize.height); 

    UILabel *lblMessage1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 1, 70, 30)]; 
    [lblMessage1 setBackgroundColor:[UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:0]]; 
    [lblMessage1 setText:message1]; 

    UILabel *lblMessage2 = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 30)]; 
    [lblMessage2 setBackgroundColor:[UIColor colorWithRed:0.3 green:0.4 blue:0.5 alpha:0]]; 
    [lblMessage2 setText:message2]; 

    UIButton *okButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [okButton setBackgroundColor:[UIColor colorWithRed:0.3 green:0.4 blue:0.3 alpha:0]]; 
    okButton.frame = CGRectMake(133, 103, 80, 25); 
    okButton.alpha = 0.1; 
    [okButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventAllTouchEvents]; 

    UIImage *imageClose = [UIImage imageNamed:@"exit1.png"]; 
    UIButton *buttonClose = [UIButton buttonWithType:UIButtonTypeCustom]; 
    buttonClose.frame = CGRectMake(320, 4, 21, 20); 
    [buttonClose setImage:imageClose forState:UIControlStateNormal]; 
    [buttonClose addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside]; 

    [self addSubview:okButton]; 
    [self addSubview:lblMessage1]; 
    [self addSubview:lblMessage2]; 
    [self addSubview:buttonClose]; 
} 

- (void) closeView{ 
    [self dismissWithClickedButtonIndex:0 animated:YES]; 
} 

- (void)dealloc { 
    [message1 release]; 
    [message2 release]; 
    [image release]; 
    [super dealloc]; 
} 


@end 

在MainGameScene類我打開第二AlertView這樣的:

-(void) showMessageAlertView:(NSString *)message1 withMessage2:(NSString *)message2 closeAlertView:(UIAlertView *)closeAlertView{ 
    [closeAlertView dismissWithClickedButtonIndex:0 animated:YES]; 

    MessageAlertView *alertView = [[MessageAlertView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)]; 

    [alertView setMessage1:message1]; 
    [alertView setMessage2:message2]; 
    [alertView setImage:[UIImage imageNamed:@"tebrikler_panel.png"]]; 

    [alertView show]; 
} 
+0

沒有一些代碼是很難猜到的東西;) – nacho4d 2010-11-22 11:50:18

+0

我加了一些代碼 – 2010-11-22 12:00:04

回答

0

我沒有一個有機會創建類似的場景來確認,但我建議您嘗試在ProductAlertView之外打開MessageAlertView

@interface MainGameScene : ParentClass <UIAlertViewDelegate> { 
    // ... 
} 

ProductAlertView設置代表顯示它由MainGameScene之前:您可以通過MainGameScene來響應UIAlertViewDelegate協議實現這一

ProductAlertView *alertView = ... 
// whatever need to be set 
[alertView setDelegate:self]; 
[alertView show]; 

和實施MainGameScene這個方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    MessageAlertView *alertView = [[MessageAlertView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)]; 

    [alertView setMessage1:@"Hello"]; 
    [alertView setMessage2:@"Hello2"]; 
    [alertView setImage:[UIImage imageNamed:@"tebrikler_panel.png"]]; 

    [alertView show]; 
} 

因此,第一個警報視圖將首先被解除,然後第二個警告視圖將被顯示。