1

I'm developing an application that allow the user to share through Facebook,Twitter and Email.

For this social networks I have created a separated class that contains this code:

-(void)Facebooksharing
{
    SLComposeViewController *facebook = [[SLComposeViewController alloc] init];
    facebook = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [facebook setInitialText:@"Hello"];
    [facebook setTitle:@"Helle"];

    ViewController *MainRoot = [[ViewController alloc] init];

    [MainRoot presentViewController:facebook animated:YES completion:nil];

}

Whenever I call this function I'm getting this Error:

Warning: Attempt to present <SLComposeViewController: 0x7ffc5afe70e0> on <ViewController: 0x7ffc5af57a60> whose view is not in the window hierarchy!

Anyone can help me please how I can present this view from another class?

Ajith Renjala
  • 4,934
  • 5
  • 34
  • 42
Ahmed jamal
  • 73
  • 1
  • 1
  • 5
  • Maybe you should try this: `[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:facebook animated:YES completion:nil];` – Bannings Aug 13 '15 at 07:28

4 Answers4

0

The issue is that you are creating a new view controller called "Main" and that view controller is not added into the windows hierarchy of your application.

Assuming your Facebooksharing method is implemented inside a view controller class. You would call

[self presentViewController:facebook animated:YES completion:nil]

Ideally the view controller that the user is currently interacting with is the one you should use to call "presentViewController".

Another time this issue can crop up if you try to present a view controller inside the viewDidLoad() method.

M.Scherzer
  • 921
  • 7
  • 9
0

if you write this code in a controller which is currently being shown to user, you will not get any error. presentViewController assumes that the controller which is presenting another controller does already have a view in the app's UIWindow.

the problem is the MainRoot is actually itself never presented to user and hence it is not eligible to present any other view controller through itself

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
0

As per your statement:

For this social networks i have created a separated class that contain this code

We don't have information of the type of this class, Is it UIViewController Or NSObject etc?

It is decided that you can not present a viewcontroller to the UINavigationController from the class other than UIViewController since self.navigationController is not accessible there.

Have a look at my answer here.

I used decorator design pattern to transfer the control from the class (say UIView, UITableViewCell, NSObject etc) to the class which is inherited form UIViewController. Then you can easily call

   [self.navigationController presentViewController:facebook animated:YES completion:nil];

In the same link you will find another answer that help to present a viewcontroller on a window. You can give that a try as well.

Regarding the problem you are actually having is, here:

ViewController *MainRoot = [[ViewController alloc] init];
[MainRoot presentViewController:facebook animated:YES completion:nil];

Here, MainRoot controller is a newly allocated viewcontroller and it was never pushed to the navigation stack, so using this to push another viewcontroller on the stack is raising this error:

Warning: Attempt to present <SLComposeViewController: 0x7ffc5afe70e0> on <ViewController: 0x7ffc5af57a60> whose view is not in the window hierarchy!

Hope it helps!

Community
  • 1
  • 1
NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

If you want to present the view controller from another class in MainRoot, then

First you have to present the MainRoot view controller from the self , and on top of that you can present your SLComposeViewController.

Vikas Dadheech
  • 1,672
  • 12
  • 23