0

In my main view controller, I have a button that calls a popover. Since the popover has it's own view controller, its buttons call methods in the popover's view controller. But how would I call a method from the main view controller?

I tried this. In the popover view controller I added a property in the .h

@class ViewController;

@interface PopoverContent : UIViewController <UITextFieldDelegate>
...
@property (strong, nonatomic) ViewController *parentView;

In my popover implementation I did this in viewDidLoad:

self.parentView = [[ViewController alloc] initWithNibName:nil bundle:nil];

In ViewController I have a method called generateHash so I tried

[parentView generateHash];

But I get the error:

No visible @interface for 'ViewController' declares selector 'generateHash'

Any idea what I'm doing wrong? Thanks

4

3 に答える 3

1

remove this line, don't want to create a new instance of view controller

self.parentView = [[ViewController alloc] initWithNibName:nil bundle:nil];

change this line

@property (strong, nonatomic) ViewController *parentView;

to this, so your parent view pointer is of the right class type

@property (weak) ParentView *parentView;

now inside parent views .m file

- (void) createPopup
{
    PopoverView *popoverV = [[PopoverView alloc] init];
    popoverV.parentView = self;

    //And some command to show your popup, addSubview, or presentModal, or whatever
}

Then in PopoverView.m file, you can call methods of the parentView like so

[self.parentView SomeMethod];
于 2012-08-28T16:54:19.437 に答える
1

This is a good place to use a delegate protocol. In the PopoverContent.h, add something like this:

@protocol PopoverContentDelegate : NSObject
    - (void) method1;
@end

Naturally, you can have more than one method, and the method(s) can return values and take parameters like any other method. Also, in the same file, add property called delegate. (Technically, it can be called anything, but everyone who looks at your code will know exactly what you're doing if you call it delegate.)

@interface PopoverContent

@property (weak) id<PopoverContentDelegate > delegate;
//other properties and methods
@end

Finally, in your "main" view controller's .m file, import PopoverContent.h file and set the delegate to self. Also implement method1 to do whatever you need it to do.

//Create the view controller
myPopoverContentController.delegate = self;
//Create the popover with the view controller.

Now, in PopoverContent controller, you can call method1 on the delegate wherever you need to.

[delegate method1];
于 2012-08-28T16:59:35.617 に答える
0

First, to answer your question, you probably have to define the generateHash method in your ViewController.h file.

Second, I'd suggest that your design approach is not optimal. The generateHash method probably needs to be in another file that can be called from both your ViewController and Popover content controller. For example consider another objective-c .h/.m pair "MyHashMethods":

    MyHashMethods.h

    + (void)generateHash;

    MyHashMethods.m

    + (void) generateHash
    {
         // hash code
    }

This would allow you to just include MyHashMethods.h in whatever view controllers you need and then call

    [MyHashMethods generateHash];

when you need it.

于 2012-08-28T17:06:02.850 に答える