lemontwist
lemontwist

Reputation: 301

Can I have a block of code run only if user is on iPhone vs iPad?

I have a master-detail app that is working great for the iPad. However, the iPhone version doesn't work because a variable that is being sent to the DetailViewController in the iPad version doesn't send to the iPhone DetailViewController. I can fix this with a single line of code in MasterViewController implementation:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    self.detailViewController=segue.destinationViewController;
}

Unfortunately, when I implement that code, the iPad version stops working. I get an exception when I go from another view controller (HomeViewController) back to DetailViewController. That error log is:

2012-07-14 14:29:12.924 46 Tracker[2772:11603] -[HomeViewController setDetailItem:]: unrecognized selector sent to instance 0x7cad4f0
2012-07-14 14:29:12.925 46 Tracker[2772:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HomeViewController setDetailItem:]: unrecognized selector sent to instance 0x7cad4f0'
*** First throw call stack:
(0x148a022 0x201acd6 0x148bcbd 0x13f0ed0 0x13f0cb2 0x2cb0 0x2d75c5 0x2d77fa 0xb6c85d 0x145e936 0x145e3d7 0x13c1790 0x13c0d84 0x13c0c9b 0x16a07d8 0x16a088a 0x246626 0x1fdd 0x1f45)
terminate called throwing an exception(lldb) 

So, is there any way I can run that first block of code only when the user is on the iPhone? Or, can I fix the code to make it work properly on both devices?

Here is a link to my iPad storyboard to (hopefully) make it more clear. I have a problem when I click on a table cell AFTER going from HomeViewController back to DetailViewController: http://www.grapekeeper.com/storyboards.png

Upvotes: 1

Views: 172

Answers (1)

John
John

Reputation: 505

Perhaps the following?

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // perform iPad logic
} else {
    // perform iPhone logic
}

Upvotes: 4

Related Questions