switch view controller has a black screen - ios

I write code in IB action to check condition to switch view controller in same story board but if I touch button it go to black screen
this is my code
-(IBAction)Buyvoyage{
if (ck==1) {
NSLog(#"brought IAP");
VoyageplayViewController *voy = [[VoyageplayViewController alloc]init];
[self presentViewController:voy animated:YES completion:NULL];
}
}

try replacing
[self presentViewController:voy animated:YES completion:NULL];
with
** [self presentViewController:voy animated:YES completion:nil];**
However, i would prefer you to use seuge by following below steps.
1. Create segue from each UIButton to the corresponding UIViewControllers, set Segue identifier in IB by selecting the segue(s) you just created e.g. if you have 2 button then set identifier of the Segue "TestSegue1" and "TestSegue2" respectively.
2. Now create an IBaction for UIButton from which you want to navigate, and put below code in that IBAction
3. Remember to set Tag property in IB of all the UIButton(s) you want to perform navigation on as per your requirement
// When any of my buttons is pressed, push the next UIViewController using performSeugeWithIdentifier method
- (IBAction)buttonPressed:(UIButton*)sender
{
if (sender.tag==1) {
[self performSegueWithIdentifier:#"TestSegue1" sender:sender];
}
else if(sender.tag==2)
[self performSegueWithIdentifier:#"TestSegue2" sender:sender];
}
Or Use storyboard identifier to present your viewcontroller using below code
- (IBAction)buttonPressed:(UIButton*)sender
{
//Get StoryBoard Identifier using
UIStoryboard *storyboard = [self storyboard];
if(ck==1) {
VoyageplayViewController *voyVC = [storyboard instantiateViewControllerWithIdentifier:#"Voyage"];
[voyVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:voyVC animated:YES];
}
}
don't forget to set ViewController StoryBoard Id in StoryBoard -> identity inspector

Related

iOS: segue for iPhone only

Is there way to make show segue works only on iPhone? Some kind off turn it off at iPad? I have show segue on iPhone and it works perfect but on iPad I am using UISplitVC with same VC. And now every time when I choose something on left part of UISplitVC it make that segue instead my left UITableVC.
Set an identifier for the segue in Interface builder, then in your view controller class you can use this in a method instead of presenting it in your storyboard. You can do that by clicking the class button in your view controller and entering it in "Storyboard ID" as shown here.
IF YOU HAVE A BUTTON THAT YOU CLICK to present the new view you can drag an IBAction from the storyboard to this method on the view controller you are presenting it from.
-(IBAction)launchIphoneOnlyController (id)sender {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self presentViewController:vc animated:true completion:nil];
}
}
Otherwise make a call to the following method from wherever you want to instantiate the view from.
-(void)launchIphoneOnlyController {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self presentViewController:vc animated:true completion:nil];
}
}
In shouldPerformSegueWithIdentifier check the seque ID you set in the story board and check (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) for iPhone.
See apple developer.

Programmatically Run prepareForSegue

I want to run some code in the prepareForSegue. The problem is that I am changing views programmatically, so the prepareForSegue function is not being run. I also don't know how to run it because I can't give it an identifier.
prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"hi");
}
Code I was trying to use to call the function manually:
[self performSegueWithIdentifier:#"test" sender:self];
Code I'm using to change between views:
UIViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:#"gameOverPage"];
[self presentViewController:second animated:YES completion:nil];
I guess if I can just set an identifier for the segue, my problem would be solved, but I don't know how to do that.
If you're not using Storyboard, you can't use the segue related methods.
Instead, just configure your controller before you present it.
UIViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:#"gameOverPage"];
// configure your controller
second.propertyA = value;
[self presentViewController:second animated:YES completion:nil];
You can set the Identifier for a segue in Storyboard: Just click on the segue and choose the attribute inspector (right side of Xcode), there you find a textfield "Identifier". Hier you can put your individual name of a identifier.

UITabBar disappears after pushed to new view controller

I have an UITabBarController that has 3 buttons. The second button points to ViewController1 which is connected to another view called ViewController2. After I tap a button in ViewController2 I programmatically present ViewController1 again, that works perfect except one thing. After I "arrived" to ViewController1 the tab bar disappears.
I'm using this method to navigate back to ViewController1. (exactly I navigate to its navigation controller, but already tried with the view)
- (void)presentViewControllerAnimated:(BOOL)animated {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard" bundle:nil];
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
}
I call here the first method
- (void)didTapButton:(id)sender {
UIButton *button = (UIButton *)sender;
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];
[self presentViewControllerAnimated:YES];
}
This method hides the tab bar in the ViewController2, I already tried without it, therefore there is no problem with it.
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}
I can't figure out why this thing happens, I think it's a fair solution, that worked well for a several times when I needed to present views. I've read it can happen with segues, but I'm doing it with code without segues.
Actually your code works right. There should not be tab bar when you present FirstViewController from SecondViewController. Because when you call instantiateViewControllerWithIdentifier its basically creates a new instance of that view controller, and of course, there is no tab bar.
The right way to go back to your first view controller is to pop SecondViewController (or dismiss it, if it presented modally). So your final code should be like this
- (void)didTapButton:(id)sender {
// If this view controller (i.e. SecondViewController) was pushed, like in your case, then
[self.navigationController popViewControllerAnimated:YES];
// If this view controller was presented modally, then
// [self dismissViewControllerAnimated:YES completion:nil];
}
And of course, your view controller hierarchy in storyboard must be like this:
-- UINavigationController -> FirstViewController -> SecondViewController
|
->UITabBarController____|
-...
-...
I've tried the same and got the same result.
My solution was simple, on the push do this :
UINavigationController *firstViewNavigationController = [storyboard instantiateViewControllerWithIdentifier:#"destination"];
firstViewNavigationController.hidesBottomBarWhenPushed = true; // Insert this and set it to what you want to do
[self presentViewController:firstViewNavigationController animated:animated completion:nil];
and then remove your
-(BOOL)hidesBottomBarWhenPushed
{
return YES;
}

iOS: Programmatically Push Segue to Multiple View Controllers

I have one original view controller with four destination view controllers. I want to be able to push segue with a navigation controller to ALL of the destination view controllers from the original. I have tried...
- (IBAction)notificationsButtonPushed:(id)sender {
NotificationsViewController *notifications = [[NotificationsViewController alloc]init];
[self.navigationController pushViewController:notifications animated:YES];
}
- (IBAction)messagesButtonPushed:(id)sender {
MessagesViewController *messages = [[MessagesViewController alloc] init];
[self.navigationController pushViewController:messages animated:YES];
}
- (IBAction)settingsButtonPushed:(id)sender {
if (canMessage) {
SettingsViewController *settings = [[SettingsViewController alloc]init];
[self.navigationController pushViewController:settings animated:YES];
}
else {
NSLog(#"Can't Message");
}
}
- (IBAction)previewButtonPushed:(id)sender {
PreviewViewController *preview = [[PreviewViewController alloc]init];
[self.navigationController pushViewController:preview animated:YES];
}
and this just gives me an empty view controller without my UI components.
Note: I also have tired "initWithNidName:" and passed in the storyboardID of each destination view controller and it gives me Error:
'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/B7E025E5-D7D2-4FFD-B49C-E10DF5E94C44/LifePoints.app> (loaded)' with name 'preview'
I also have tried... (with storyboard segues set to "Push")
- (IBAction)notificationsButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"notifications" sender:self];
}
- (IBAction)messagesButtonPushed:(id)sender {
if (canMessage) {
[self performSegueWithIdentifier:#"messages" sender:self];
}
else {
NSLog(#"Can't Message");
}
}
- (IBAction)settingsButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"settings" sender:self];
}
- (IBAction)previewButtonPushed:(id)sender {
[self performSegueWithIdentifier:#"preview" sender:self];
}
Although this does push the destination view controllers onto the screen with the appropriate segue type, it does not segue to the correct destination view controller. It only seems to segue to the last attached storyboard segue.
Does anyone know how to correctly apply this and have it behave in the form I am looking for?
EDIT
It is important to note I am checking to see if a condition is met the "messagesButtonPushed" method. I am checking to see if user is allowed to message, and if they are, then segue to the VC.
You don't need any code to implement the basic segues.
In your story board ensure that your original view controller is embedded in a navigation controller (Select the original View and select Edit->Embed in->Navigation Controller).
Then you can simply control drag from each of your four buttons to the corresponding destination views, selecting "push" as the segue type. You can then click on the segue icon between the two views to give each segue an identifier.
You can delete your IBAction methods and any actions on the buttons that link to them.
I suggest you complete a Storyboard Tutorial to learn how storyboards and segues work.
If you do want to perform a segue programatically then you can control drag from the yellow view controller icon at the bottom of the view to the destination. You can then invoke the segue with performSegueWithIdentifier as per your second code - In your case your could have two different segues and trigger the store segue or the other one depending on purchase status

How to programatically change views in iOS

In Xcode 4.5 you can change views from a segue, But I want to programatically change views in Mainstoryboard. I Know in Xib. we use this method:
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
I want to change the view when the user clicks done in the textfield (This code detects if user clicks done):
-(IBAction)hidekeyboard {
[sender resignFirstResponder];
}
You create a segue in your storyboard that goes from the view that has the text field in question and goes to your SecondViewController. You have to assign an identifier to it (also in the storyboard, click on the line connecting the two view controllers), let's call it "segueToSecondViewController". Then, you can manually call the segue using:
[self.storyboard performSegueWithIdentifier:#"segueToSecondViewController" sender:self];
An alternative to segueing is to instantiate the viewController from the storyboard.
First you assign a Storyboard ID to the viewController in the storyboard.
Then you instantiate that viewController, and present it.
MBTagEditorViewController *tagEditor = [self.storyboard instantiateViewControllerWithIdentifier:#"TagEditor"];
[self presentModalViewController:tagEditor animated:YES];

Resources