문제

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];
도움이 되었습니까?

해결책

Try this

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

Hope it help!

다른 팁

Just use an if statement to check if the string exists, if it exists then execute your code.

NSString *emaill = [contact valueForKey:@"email"];
if (emaill != nil) {
    MFMail...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top