0

I made a contacts app for fun to dive into iOS.

Basically when you add a contact you can call email or text them.

Since i use the same viewcontroller when adding them and viewing them, how can i make it so that the buttons are disabled till the contact is viewed ie. coredata attributes not null?

For instance say I go to add a new contact, all the fields blank and i hit the email button, the app will crash since there is no entity to email cause the contact is not saved yet.

here is my working email code: I need it to not activate if the value for key email is null or empty to avoid a crash.

     NSString *emaill = [contact valueForKey:@"email"];
MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc] init];
[mailcontroller setMailComposeDelegate:self];
NSString *email =[@""stringByAppendingString:emaill];
NSArray *emailArray = [[NSArray alloc] initWithObjects:email, nil];
[mailcontroller setToRecipients:emailArray];
[mailcontroller setSubject:@""];
[self presentViewController:mailcontroller animated:YES completion:nil];
4

2 回答 2

0

尝试这个

NSString *emaill = [contact valueForKey:@"email"];
if (emaill.length>0)//if exists
{
   //your code here
}
else
{
   NSLog(@"Empty!!!");
}

希望对您有所帮助!

于 2013-07-26T05:34:02.347 回答
0

只需使用 if 语句检查字符串是否存在,如果存在则执行您的代码。

NSString *emaill = [contact valueForKey:@"email"];
if (emaill != nil) {
    MFMail...
}
于 2013-07-26T05:12:18.170 回答