0

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

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

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

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

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

主视图控制器.h

#import <UIKit/UIKit.h>

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

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

@end

主视图控制器.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];
}

菜单视图控制器.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

菜单视图控制器.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];
}
4

3 回答 3

1

在这种特殊情况下,您可以在dismissViewControllerAnimated调用之前输入以下内容,它应该可以工作:

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

通常,您真正想要的称为委托

所以,为了更简洁的实现,试试这个:

菜单视图控制器.h

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

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

菜单视图控制器.m

- (IBAction)bDonePressed {
    // ...

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

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

主视图控制器.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];
}
于 2013-03-15T12:06:44.117 回答
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];
    

请注意,method必须在 MainViewController.h 文件中声明它才能被调用。

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

于 2013-03-15T12:02:34.023 回答
0

这段代码对你有用......像这样修改你的代码......

主视图控制器.h

#import <UIKit/UIKit.h>

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

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

@end

主视图控制器.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];
}

菜单视图控制器.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

菜单视图控制器.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];
}
于 2013-03-15T12:09:53.793 回答