user2303228
user2303228

Reputation: 45

Having issues displaying modal view programmatically

I am very very new to iOS programming / Objective C.

The flow of events I want to have happen is: user selects tab from tab bar view controller. Once VIEW A has been loaded it will open a modal window to get some information

VIEW A - (void)viewDidLoad

ModalYearPickerViewController *modalYearPickerViewController= [[ModalYearPickerViewController alloc] init];

[self presentViewController:modalYearPickerViewController animated:NO completion:nil];

I am trying to have my year picker view load up right away so the user can select a year from my picker (in VIEW B), then close the modal window after the value has been passed back to VIEW A.

Now, the fist view loads, then goes to a black screen automatically. I am unsure why as my view controller for modalYearPickerViewController has a picker etc on it.

Any tips or help loading a modal view controller programmatically would be greatly appreciated!

Thanks!

Upvotes: 4

Views: 6049

Answers (2)

Cagdas Altinkaya
Cagdas Altinkaya

Reputation: 1750

If you are using storyboards :

UIStoryboard *storyBoard = [self storyboard]; 

This will return the storyboard of your current view controller. I am assuming your View Controller A is also on your storyboard.

ModalYearPickerViewController *modalYearPickerViewController  = [storyBoard instantiateViewControllerWithIdentifier:@"ModalYearPickerViewController"];

This will instantiate your view controller from the storyboard. But one other thing you have to do is set your view controllers storyboard id to ModalYearPickerViewController. You can set this right below where you set your custom view controller class in the storyboard.

[self presentViewController:modalYearPickerViewController animated:NO completion:nil];

and done.

Upvotes: 8

Mariam K.
Mariam K.

Reputation: 620

If you have a xib file for that viewContrioller, it has to be loaded as well, to do that you have to call:

ModalYearPickerViewController *modalYearPickerViewController = 
[[ModalYearPickerViewController alloc] initWithNibName:@"ModalYearPickerViewController" bundle:nil];

Upvotes: 0

Related Questions