UIAlertview 错误:无法同时满足约束
Posted
技术标签:
【中文标题】UIAlertview 错误:无法同时满足约束【英文标题】:UIAlertview error: Unable to simultaneously satisfy constraints 【发布时间】:2014-06-20 17:25:57 【问题描述】:这个问题已经被问过很多次了,但我已经尝试了所有选项,但似乎没有一个有效。在我的 ios 应用程序中,每当我尝试制作警报视图时,都会发生这种情况:
http://i61.tinypic.com/kalnk2.png
我不知道该怎么办。当我单击确定时,我收到此错误:(这是整个错误)
http://pastebin.com/raw.php?i=BEeGbjJ8
我的代码是:
@implementation AJSettingsViewController
-(NSString*) dataFilePath
NSArray *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:FILENAME];
-(void) saveFile
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:country];
[array addObject:state];
[array addObject:town];
[array addObject:zipcode];
[array addObject:name];
[array writeToFile:[self dataFilePath] atomically:YES];
//[array dealloc];
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
zipcodetextfield.translatesAutoresizingMaskIntoConstraints = NO;
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[super viewDidLoad];
// Do any additional setup after loading the view.
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(IBAction)zipcodelookup:(id)sender
[zipcodetextfield resignFirstResponder];
zipcodetextfield.translatesAutoresizingMaskIntoConstraints = NO;
zipcode = zipcodetextfield.text;
if(zipcode.length >0)
NSString *url = [NSString stringWithFormat:@"http://ziptasticapi.com/%@",zipcode];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
[connection start];
NSError *noconnection;
NSString *htmlpage = [NSString stringWithContentsOfURL:[NSURL URLWithString: url] encoding:NSASCIIStringEncoding
error:&noconnection];
//NSLog(htmlpage);
if ([htmlpage rangeOfString:@"error"].location == NSNotFound)
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@":" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"\"" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"country" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"city" withString:@""];
htmlpage = [htmlpage stringByReplacingOccurrencesOfString:@"state" withString:@""];
NSLog(htmlpage);
else
zipcodetextfield.text = @"";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"We were unable to locate this zipcode. Please try again"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
message.translatesAutoresizingMaskIntoConstraints = NO;
[message show];
else
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please Enter A ZipCode"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
message.translatesAutoresizingMaskIntoConstraints = NO;
[message show];
-(IBAction)exitkeyboard:(id)sender
[zipcodetextfield resignFirstResponder];
@end
我在这里绝对是机智,所以任何帮助将不胜感激。
谢谢
【问题讨论】:
您决定删除错误消息中最重要的部分。它将列出不能同时工作的约束。这是我们需要看到的一点。不是代码。 哎呀,让我补充一下,对不起 【参考方案1】:message.translatesAutoresizingMaskIntoConstraints = NO;
不要为UIAlertView
设置此项 - 警报视图及其私有管理器将决定其布局的工作方式。
【讨论】:
translatesAutoresizingMaskIntoConstraints
仅应在您在代码中创建视图并希望使用明确定义的约束对其进行定位时设置为 false。您的代码中似乎还有一些其他不适当的分配(self.view
和 zipcodetextfield
)。
我删除了它们。我添加了它们是因为有关此主题的其他一些帖子说这是问题所在。不知道怎么回事
如果没有这些行,它还会崩溃吗?
它没有崩溃,只是看起来很糟糕。它仍然看起来像上图。【参考方案2】:
尝试使用delegate:self
代替nil
,并在您的.h 文件中添加UIAlertViewDelegate
,使其如下所示:
@interface AJSettingsViewController : UIViewController <UIAlertViewDelegate>
【讨论】:
【参考方案3】:尝试使用 UIAlertController:
UIAlertController *myAlertController = [UIAlertController
alertControllerWithTitle:@"Title"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
带按钮:
UIAlertAction *myCancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
NSLog(@"Cancel action");
[self someGreatActionWithIndex:0];
];
UIAlertAction *myOkAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
NSLog(@"OK action");
[self someGreatActionWithIndex:1];
];
[myAlertController addAction:myCancelAction];
[myAlertController addAction:myOkAction];
希望这会有所帮助!
【讨论】:
以上是关于UIAlertview 错误:无法同时满足约束的主要内容,如果未能解决你的问题,请参考以下文章