如何从NSObject类中显示UIViewController?

时间:2012-10-19 13:56:43

标签: ios xcode4 uiviewcontroller storyboard nsobject

有一个当前视图UIViewController调用“LoginView”,但我不在,我在NSObject类,我想打电话,显示另一个{{1}这叫做“MapView”。我该怎么做?enter image description here

问题就像上面的截图一样。

6 个答案:

答案 0 :(得分:10)

在您的IBAction或具体方法中写下:

UIWindow *window=[UIApplication sharedApplication].keyWindow;
UIViewController *root = [window rootViewController];

UIStoryboard *storyboard = root.storyboard;
CustomViewController *vcc =(CustomViewController *) [storyboard instantiateViewControllerWithIdentifier:@"storyBoardID"];

[root presentModalViewController:vcc animated:YES];

答案 1 :(得分:5)

我假设您正在尝试从NSObject类的UIViewController类访问您的UIViewController成员。简单地将UIViewController成员传递给NSObject类。在这种情况下自我。让你做的是你可以从另一个类中更改,编辑,删除你想要在你的UIView中做的任何事情。以下是一个例子。

从UIViewController类调用NSObject类

@implementation myViewControllerClass

- (void) viewDidLoad {
    //Pass in the UIViewController object, in this case  itself. 
    [[myNSOBjectClass alloc] startViewController:self]; 
    ....
}

然后从你的NSObject

@interface myNSOBjectClass{
    //Global access to the view controller. 
    UIViewController *viewController; 
}
...

@implementation myNSOBjectClass
...

//function that the caller calls to pass their UIViewController object 
- (void)startViewController:(UIViewController *)callerViewController{
    viewController = [[UIViewController alloc]init];
    //puts the caller's view controller to the global member.
    viewController = callerViewController;  
    ...
}

现在,您可以轻松获得视图控制器!

干杯:)!

答案 2 :(得分:2)

您不应该在模型中实例化和显示视图控制器。视图应该由模型驱动

在这种情况下,您提到了LoginView作为起点。如果满足某些条件(可能成功登录?),您应该相应地更新基础模型,然后显示MapView

来自LoginView

MapView *mapView = [[MapView alloc] init];

如果您的应用使用导航控制器:

[self.navigationController pushViewController:mapView animated:YES];

否则:

[self presentViewController:mapView animated:YES completion:<nil or block>];

答案 3 :(得分:2)

试试此代码。它会帮助你......

在您的按钮单击操作中您必须发送您的UINavigationController&amp;当前的ViewController。因为NSObject类没有找到那个控制器。

在你的Button Action中输入此代码:

[demo login_method_called:self.navigationController withCurrentViewController:self];

在您的NSObject .h类中输入以下代码:

#import <Foundation/Foundation.h>
#import "Home_ViewController.h"

@interface Method_Action_class : NSObject

- (void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller;
@end

在你的NSObject .m类中输入以下代码:

#import "Method_Action_class.h"

@implementation Method_Action_class

-(void)login_method_called:(UINavigationController*)navigation  withCurrentViewController:(UIViewController*) controller
{
    Home_ViewController *home = [[Home_ViewController alloc] initWithNibName:@"Home_ViewController" bundle:nil];
    [navigation pushViewController:home animated:YES];
}
@end

构建代码。

答案 4 :(得分:0)

我在视图控制器类中创建了一个obersver,并使用来自NSObject子类的NSNotification中心声明了推送视图控制器和发布通知的方法,并且它运行良好。

视图控制器中的

: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissPickerView)name:kNotificationDismissPicker object:nil];

在NSOject的子类中:  [[NSNotificationCenter defaultCenter] postNotificationName:kMoveToAchievementView对象:nil];

答案 5 :(得分:0)

我在我的NSObject类中使用了这样的东西:

                List<int> numbers = new List<int>();
                private void buttonAdd_Click(object sender, EventArgs e)
                {
                    if (radioButtonSorted.Checked)
                    {
                        numbers.Clear();

                        if (!string.IsNullOrEmpty(textBoxInsert.Text))
                            numbers.Add(int.Parse(textBoxInsert.Text));

                        foreach (var item in listBoxAddedIntegers.Items)
                        {
                            if (item != null)
                                numbers.Add(int.Parse(item.ToString()));
                        }

                        listBoxAddedIntegers.Items.Clear();
                        numbers.Sort();
                        foreach (var number in numbers)
                            listBoxAddedIntegers.Items.Add(number);
                    }
                    else
                        listBoxAddedIntegers.Items.Add(textBoxInsert.Text);

                    textBoxInsert.Text = string.Empty;
                    //MessageBox.Show(listBoxAddedIntegers.SelectedIndex.ToString());
                    MessageBox.Show(listBoxAddedIntegers.Items.Count.ToString());
                    }

我希望它有用。