简体   繁体   中英

Why self.view is not available

I have this code in the main view controller and it is working just fine and as I wanted.

loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting To Twitter..."
                                                 delegate:nil
                                        cancelButtonTitle:@"Cancel"
                                   destructiveButtonTitle:nil 
                                        otherButtonTitles:nil];

[loadingActionSheet showInView:self.view];

I wanted that code to be reusable from different part of the project so I moved it in a separate file (UIView based).

The poblem that I am facing is that self.view is not available there and I don't know why because I am learning and I don't know enough to understand what I am missing.

What do I have to do/add/change to have the actionsheet shown in my current view even if that code lives somewhere else?

Sounds like you don't quite understand Objective-C and object oriented programming yet.

self is just like this in other OO languages. That is, when executing a method, the self variable inside the method is a means of communicating with the instance of the Class or Subclass within which the method is implemented.

If the class of self does not have a view property, self.view won't compile. Even if it does have a view property, it might not be the right view!

I would suggest you read and re-read the Objective-C guide a few times (I read it once a year the first five years I was doing Cocoa development -- predecessors to Cocoa, anyway).

Once you grok Objective-C, then you need to think about how your application is put together. How all the objects are connected together, in particular. Or, more specifically, when you broke your application up into separate files, what classes do those files define and how do the instances of those classes fit together in your application?

Do you mean something like this?:

void actionSheetShownIn(UIView *target) {
  UIActionSheet *loadingActionSheet = [[UIActionSheet alloc] initWithTitle:@"Posting To Twitter..."
                                             delegate:nil
                                    cancelButtonTitle:@"Cancel"
                               destructiveButtonTitle:nil 
                                    otherButtonTitles:nil];

  [loadingActionSheet showInView: target];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM