我几天都在努力解决这个问题。
我有一个自定义设计的UIAlertView(一个新的UIAlertView继承类,其名称为ProductAlertView),我想打开一个新的自定义设计的UIAlertView(也是一个UIAlertView继承的类)。当我在自定义设计的alertview中创建这个新的自定义设计的UIAlertView时,看不到新设计的alertview(一些图像丢失......) 另外,我尝试实现一个单例类,它关闭第一个警报视图,然后打开第二个警报视图;然而,并没有改变这种观点没有被充分和充分地看到。
有人有解决方案吗?
一些代码:
First 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
第二次警报视图
#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 :(得分:0)
我没有机会创建类似的方案进行确认,但我建议您尝试在MessageAlertView
之外打开ProductAlertView
。您可以通过MainGameScene
回复UIAlertViewDelegate
协议来实现此目的:
@interface MainGameScene : ParentClass <UIAlertViewDelegate> {
// ...
}
并在ProductAlertView
显示MainGameScene
之前设置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];
}
中实施此方法:
{{1}}
因此,第一个警报视图将首先被解除,然后第二个警报视图将被显示。