如何在现有对象上调用来自不同类的方法(Obj C)

时间:2013-03-15 11:51:15

标签: objective-c xcode uiviewcontroller

我对Objective C和Xcode很新,可能只是做了一些愚蠢的错误,但我尝试了很多不同的建议而无法让它们起作用。

简而言之,我想在现有的UIViewController上调用一个方法,当在另一个UIViewController中按下一个按钮时。

(有点背景:两个屏幕是主屏幕菜单屏幕。用户向上滑动以查看菜单,然后当完成按钮时按下我希望菜单屏幕将一个字符串变量(在这种情况下为URL)发送到主屏幕中的方法,然后主屏幕在UIWebView中显示URL。

我尝试将方法/ URL变量放在AppDelegate / MainViewController中并合成它们。我还尝试使MenuViewController成为MainViewCOntroller的子类,并使用[super readFile:url]调用该方法,该方法已编译但我认为它不会调用现有的MainViewController对象,因为webview没有出现。 / p>

我将粘贴下面代码的相关位:(感谢您的帮助!)

MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController {
IBOutlet UIWebView *vCodeView;
}

- (void)readFile:(NSString *)newURL;

@end

MainViewController.m

#import "MainViewController.h"
#import "MenuViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)readFile:(NSString *)newURL {
    vCodeView.hidden = NO;

    // seperate url by full stops
    NSArray *splitURL = [newURL componentsSeparatedByString:@"."];
    // find extension of file
    NSString *extension = [splitURL objectAtIndex:([splitURL count]-1)];

    // DO SOME OTHER STUFF WITH URL THEN DISPLAY CONTENT IN UIWEBVIEW
}

- (void)swipeUpRecognised {
    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    menuView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:menuView animated:YES completion:nil];
}

MenuViewController.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@interface MenuViewController : UIViewController {
    IBOutlet UIBarButtonItem *bDone;
    IBOutlet UITextField *fUser;
    IBOutlet UITextField *fPass;
    IBOutlet UITextField *fAddress;
    IBOutlet UITextField *fAutoConnectAddress;
}

- (IBAction)bDonePressed;

@end

MenuViewController.m

#import "MenuViewController.h"
#import "MainViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

- (IBAction)bDonePressed {

    NSString *user = @"admin";
    NSString *pass = @"password";
    NSString *path = @"54.246.90.95/repos/project1/trunk/dir1/test.c";

    // url of file
    NSArray *pathArray = [[NSArray alloc] initWithObjects:@"http://", user, @":", pass, @"@", path, nil];
    NSString *url = [pathArray componentsJoinedByString:@""];

    //call the method here passing in url

    [self dismissViewControllerAnimated:YES completion:nil];
}

3 个答案:

答案 0 :(得分:1)

在这种特殊情况下,您可以在dismissViewControllerAnimated来电之前输入以下内容,它应该有效:

if(self.presentingViewController && [self.presentingViewController respondsToSelector:@selector(readFile:)]) {
    [(id)self.presentingViewController readFile:url];
}

一般来说,您真正想要的是Delegation

因此,为了更清洁的实现,请尝试以下方法:

MenuViewController.h

@protocol MenuViewControllerDelegate <NSObject>
    - (void)menuViewControllerFinishedWithURL:(NSURL *)url;
@end

@interface MenuViewController : UIViewController {
    @property id<MenuViewControllerDelegate> delegate;
// ...

MenuViewController.m

- (IBAction)bDonePressed {
    // ...

    if(self.delegate) {
        [self.delegate menuViewControllerFinishedWithURL:url];
    }

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

MainViewController.m

@interface MainViewController () <MenuViewControllerDelegate>

@end

// ...

- (void)swipeUpRecognised {
    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
    menuView.delegate = self;
    menuView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:menuView animated:YES completion:nil];
}

答案 1 :(得分:0)

为了调用MainViewController方法,它应该很简单:

  1. 将MainViewController.h包含在MenuViewController.h中。
  2. 将此代码段称为:

    NSString * vcid = @""; //put id you have assigned to your MainViewController. This should be in your storyboard. If it is empty, fill it with proper name, then use it here.
    UIStoryboard *storybord = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    MainViewController *mainvc =[storybord instantiateviewcontrollerwithidentifier:vcid];
    [mainvc method];
    
  3. 请注意,必须在MainViewController.h文件中声明method才能调用它。

    那就是说,我想知道你的设计是否真的需要为菜单设置单独的视图控制器。如果您想为选择提供一些按钮组,请考虑包含所有这些按钮的UIView。然后应该在swipeUpRecognised中弹出这个UIView。

答案 2 :(得分:0)

此代码适合您... 像这样修改你的代码....

<强> MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController {
IBOutlet UIWebView *vCodeView;
}

- (void)readFile:(NSString *)newURL;

@end

<强> MainViewController.m

#import "MainViewController.h"
#import "MenuViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)readFile:(NSString *)newURL {
    vCodeView.hidden = NO;

    // seperate url by full stops
    NSArray *splitURL = [newURL componentsSeparatedByString:@"."];
    // find extension of file
    NSString *extension = [splitURL objectAtIndex:([splitURL count]-1)];

    // DO SOME OTHER STUFF WITH URL THEN DISPLAY CONTENT IN UIWEBVIEW
}

- (void)swipeUpRecognised {
    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
menuView.delegate = self;
    menuView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentViewController:menuView animated:YES completion:nil];
}

<强> MenuViewController.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@interface MenuViewController : UIViewController {
    IBOutlet UIBarButtonItem *bDone;
    IBOutlet UITextField *fUser;
    IBOutlet UITextField *fPass;
    IBOutlet UITextField *fAddress;
    IBOutlet UITextField *fAutoConnectAddress;
}
@property(nonatomic, retain) MainViewController *delegate;
- (IBAction)bDonePressed;

@end

<强> MenuViewController.m

#import "MenuViewController.h"
#import "MainViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController
@synthesize delegate = _delegate;
- (IBAction)bDonePressed {

    NSString *user = @"admin";
    NSString *pass = @"password";
    NSString *path = @"54.246.90.95/repos/project1/trunk/dir1/test.c";

    // url of file
    NSArray *pathArray = [[NSArray alloc] initWithObjects:@"http://", user, @":", pass, @"@", path, nil];
    NSString *url = [pathArray componentsJoinedByString:@""];

    //call the method here passing in url
    [self.delegate readFile:url];

    [self dismissViewControllerAnimated:YES completion:nil];
}