我做了一个自定义的 UIAlertView,看起来像这样
http://www.odlgs.com/customAlertBefore.jpg
问题是当我旋转到另一个界面方向时,警报会改变它的大小,看起来像这样:
http://www.odlgs.com/customAlertAfter.jpg
我制作了一个 NSNotification,它允许我在切换到另一个界面方向时调整控件的大小,但不适用于我的 UIAlertView,并且 autoresizingMask 属性也不起作用,有遇到同样问题或有一些想法的人吗?这是代码:
/*** THIS IS THE CLASS OF MY CUSTOM ALERTVIEW ***/
#import "AlertTableViewController.h"
#import "Theme.h"
@implementation AlertTableViewController
@synthesize textField, caller, context, data;
-(id)initWithCaller:(id)_caller data:(NSArray*)_data title:(NSString*)_title andContext:(id)_context{
NSMutableString *messageString = [NSMutableString stringWithString:@"\n"];
tableHeight = 0;
if([_data count] != 0){
for(int i = 0; i < [_data count]; i++){
//[messageString appendString:@"\n\n"];
//tableHeight += 53;
tableHeight = 150;
}
}else{
//messageString = @"\n\n\n\n\n\n\n\n\n\n";
tableHeight = 207;
}
if(self = [super initWithTitle:_title message:messageString delegate:self cancelButtonTitle:nil otherButtonTitles:nil]){
self.caller = _caller;
self.context = _context;
self.data = _data;
[self prepare];
}
return self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[self.caller didSelectRowAtIndex:-1 withContext:self.context];
}
-(void)show{
self.hidden = YES;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(myTimer:) userInfo:nil repeats:NO];
[super show];
}
-(void)myTimer:(NSTimer*)_timer{
self.hidden = NO;
[myTableView flashScrollIndicators];
}
//HERE I ADD THE CONTROLS TO MY CUSTOM ALERTVIEW
-(void)prepare{
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
textField = [[UITextField alloc] initWithFrame:CGRectMake(11, 50, 261, 30)];
textField.backgroundColor = [UIColor whiteColor];
textField.delegate = self;
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(11, 115, 261, tableHeight) style:UITableViewStylePlain];
myTableView.scrollEnabled = NO;
myTableView.delegate = self;
myTableView.dataSource = self;
UILabel *sourcesTitle = [[UILabel alloc] initWithFrame:CGRectMake(11, 90, 261, 20)];
sourcesTitle.text = @"Source";
sourcesTitle.textColor = [UIColor whiteColor];
sourcesTitle.backgroundColor = [UIColor clearColor];
sourcesTitle.font = [UIFont fontWithName:kLKTtFONT_NAME size:kLKTtFONT_LABEL_SIZE_17];
UIButton *ok = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ok.frame = CGRectMake(11, 280, 120, 30);
[ok setTitle:@"Ok" forState:UIControlStateNormal];
[ok addTarget:self action:@selector(saveNetwork) forControlEvents:UIControlEventTouchDown];
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancel.frame = CGRectMake(150, 280, 120, 30);
[cancel setTitle:@"Cancel" forState:UIControlStateNormal];
[cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchDown];
[self addSubview:sourcesTitle];
[self addSubview:ok];
[self addSubview:cancel];
[self addSubview:myTableView];
[self addSubview:textField];
[textField becomeFirstResponder];
UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, 50, 261, 4)] autorelease];
imgView.image = [UIImage imageNamed:@"top.png"];
[self addSubview:imgView];
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(11, tableHeight+46, 261, 4)] autorelease];
imgView.image = [UIImage imageNamed:@"bottom.png"];
[self addSubview:imgView];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 10);
[self setTransform:myTransform];
}
-(void)saveNetwork{
CreateNetworkCommunication *communication = [[CreateNetworkCommunication alloc] init];
communication.sourceId = [[[data objectAtIndex:selectedSource] valueForKey:@"Id"] integerValue];
communication.name = textField.text;
communication.delegate = self;
[communication doRequest];
}
-(void)finishSaveNetworkRequestWithResponse:(OwnerResult*)response{
if (response.StatusCode == 0) {
[super dismissWithClickedButtonIndex:0 animated:YES];
[self.caller refreshNetworks];
}
else {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:self.title message:NSLocalizedString(@"alert.error-saving", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles: nil];
[error show];
[error release];
}
}
-(void)cancel{
[super dismissWithClickedButtonIndex:1 animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"ABC"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"ABC"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.font = [UIFont boldSystemFontOfSize:14];
}
if (selectedSource != indexPath.row)
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [[data objectAtIndex:indexPath.row] valueForKey:@"Name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//[self dismissWithClickedButtonIndex:0 animated:YES];
//[self.caller didSelectRowAtIndex:indexPath.row withContext:self.context];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedSource = indexPath.row;
[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
selectedSource = indexPath.row;
cell.accessoryType = UITableViewCellAccessoryNone;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
-(void)dealloc{
self.data = nil;
self.caller = nil;
self.context = nil;
[myTableView release];
[super dealloc];
}
/*** THIS IS THE VIEWCONTROLLER WHERE I IMPLEMENT THE CUSTOM ALERTVIEW ***/
#import "NetworkSelectorViewController.h"
@interface NetworkSelectorViewController ()
@end
@implementation NetworkSelectorViewController
@synthesize delegate, alert;
-(void)add {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(resizeTaxonomyView:) name:@"resizeTaxonomyView" object:nil];
alert = [[AlertTableViewController alloc] initWithCaller:self data:sources title:@"New Network" andContext:nil];
//UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"button.new", nil), self.title] message:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"button.cancel", nil) otherButtonTitles:NSLocalizedString(@"button.save", nil), nil];
//alert.alertViewStyle = UIAlertViewStylePlainTextInput;
//UITextField * alertTextField = [alert textFieldAtIndex:0];
//UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(11, 110, 261, 200) style:UITableViewStylePlain];
//UIButton* okButton = [[UIButton alloc] initWithFrame:CGRectMake(11, 30, 50, 50)];
//[alert addSubview:okButton];
//okButton.frame = CGRectMake(okButton.frame.origin.x, 400, okButton.frame.size.width, okButton.frame.size.height);
//alertTextField.keyboardType = UIKeyboardTypeAlphabet;
//alertTextField.placeholder = self.title;
//[alert addSubview:tableView];
//alert.data = [[NSArray alloc] initWithObjects:@"1",@"2", nil];
alert.delegate = self;
NSLog(@"ALERT SUBVIEWS: %@", alert.subviews);
[alert show];
alert.frame = CGRectMake(alert.frame.origin.x, alert.frame.origin.y, alert.frame.size.width, 370);
NSLog(@"X:%f Y:%f W:%f H:%f", alert.frame.origin.x, alert.frame.origin.y, alert.frame.size.width, alert.frame.size.height);
UIButton* button1 = (UIButton*)[alert.subviews objectAtIndex:2];
button1.frame = CGRectMake(0,300, 50, button1.frame.size.height);
//[alert release];
}
- (void)resizeTaxonomyView:(NSNotification *)notification {
// El mÈtodo recibe un objeto de tipo NSNotification cuya propiedad object
// alberga el objeto pasado como parametro. En este caso hacemos un casting
// del objeto a NSString.
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
alert.frame = CGRectMake(alert.frame.origin.x, alert.frame.origin.y, alert.frame.size.width, 500);
else
alert.frame = CGRectMake(alert.frame.origin.x, alert.frame.origin.y, 500, 1000);
}
NSnotification 有效,方法触发,但 alertview 不自动调整大小