开发者

Why self.view is not available

开发者 https://www.devze.com 2023-01-01 06:33 出处:网络
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...\"

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 开发者_如何学Pythonto 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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消