iPhone/Objective-c - 从子视图访问先前的 ViewController?

发布于 2024-11-19 06:47:28 字数 3402 浏览 1 评论 0原文

如何在子视图中访问上一个视图控制器?因为我能够执行操作,但我无法使用 self.[我的主视图控制器]。

这是我的代码,用于测试目的:

PhotoViewController.m

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                                 self.label.text,
                                                 num];

    //this part doesn't work
    //switch over to the third view to see if it worked
    self.tabBarController.selectedIndex = 0;
}

我有一个 UITabBarController ,它的视图控制器之一有一个 UIScrollViewUIScrollView 内部是一个 PhotoViewController 对象。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch
    MyTabBarViewController *vc2 = [[MyTabBarViewController alloc] init];
    SecondViewController *vc3 = [[SecondViewController alloc] init];
    controller = [[DemoAppViewController alloc] init];
    controller.view.frame = CGRectMake(0, 20, 320, 460);

    controller.title = @"Intro Screen";
    vc2.title = @"Explore";
    vc3.title = @"Send a Pic";
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = [NSArray arrayWithObjects:controller, vc2, vc3, nil];
    [controller release];
    [vc2 release];
    [vc3 release];

    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];

  return YES;

}

另外,我的 TabBarViewController.m // 不是我实际的 UITabBarController,措辞令人困惑

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    arrayCount = [array count];
    scroller.delegate=self;
    scroller.pagingEnabled=YES;
    scroller.directionalLockEnabled=YES;
    scroller.showsHorizontalScrollIndicator=NO;
    scroller.showsVerticalScrollIndicator=NO;

    //should have an array of photo objects and the number of objects, correct?
    scrollWidth = 0;
    scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);

    for (int i = 0; i < arrayCount;i++) {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];        
        UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectOffset(scroller.bounds, scrollWidth, 0)];
        CGRect rect = scrollImageView.frame;
        pvc.view.frame  = rect;
        pvc.label.textColor = [UIColor whiteColor];
        id individualPhoto = [array objectAtIndex:i];
        NSLog(@"%@",individualPhoto);
        NSArray *keys=[individualPhoto allKeys];
        NSLog(@"%@",keys);
        NSString *imageURL=[individualPhoto objectForKey:@"source"];
        NSURL *url = [NSURL URLWithString:imageURL];
        NSData  *data = [NSData dataWithContentsOfURL:url];
        pvc.imageView.image = [[UIImage alloc] initWithData:data];
        pvc.label.text = [individualPhoto objectForKey:@"id"];
        [scroller addSubview:pvc.view];
        [scrollImageView release];
        //[pvc release];
        scrollWidth += scroller.frame.size.width;
    }

    if (arrayCount > 3) {
        pageControl.numberOfPages=3;
    } else {
    pageControl.numberOfPages=arrayCount;
    }
    pageControl.currentPage=0;
}

How do I access a previous view controller while in a subview? Because I'm able to perform actions but I'm just not able to use self.[my main view controller].

This is my code, for testing purposes:

PhotoViewController.m

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                                 self.label.text,
                                                 num];

    //this part doesn't work
    //switch over to the third view to see if it worked
    self.tabBarController.selectedIndex = 0;
}

I have a UITabBarController and one of its view controllers has a UIScrollView. Inside of the UIScrollView is a PhotoViewController object.

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch
    MyTabBarViewController *vc2 = [[MyTabBarViewController alloc] init];
    SecondViewController *vc3 = [[SecondViewController alloc] init];
    controller = [[DemoAppViewController alloc] init];
    controller.view.frame = CGRectMake(0, 20, 320, 460);

    controller.title = @"Intro Screen";
    vc2.title = @"Explore";
    vc3.title = @"Send a Pic";
    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = [NSArray arrayWithObjects:controller, vc2, vc3, nil];
    [controller release];
    [vc2 release];
    [vc3 release];

    [self.window addSubview:tbc.view];
    [self.window makeKeyAndVisible];

  return YES;

}

Also, my TabBarViewController.m //not my actual UITabBarController though, wording is confusing

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    arrayCount = [array count];
    scroller.delegate=self;
    scroller.pagingEnabled=YES;
    scroller.directionalLockEnabled=YES;
    scroller.showsHorizontalScrollIndicator=NO;
    scroller.showsVerticalScrollIndicator=NO;

    //should have an array of photo objects and the number of objects, correct?
    scrollWidth = 0;
    scroller.contentSize=CGSizeMake(arrayCount*scroller.frame.size.width, scroller.frame.size.height);

    for (int i = 0; i < arrayCount;i++) {
        PhotoViewController *pvc = [[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil];        
        UIImageView *scrollImageView = [[UIImageView alloc] initWithFrame:CGRectOffset(scroller.bounds, scrollWidth, 0)];
        CGRect rect = scrollImageView.frame;
        pvc.view.frame  = rect;
        pvc.label.textColor = [UIColor whiteColor];
        id individualPhoto = [array objectAtIndex:i];
        NSLog(@"%@",individualPhoto);
        NSArray *keys=[individualPhoto allKeys];
        NSLog(@"%@",keys);
        NSString *imageURL=[individualPhoto objectForKey:@"source"];
        NSURL *url = [NSURL URLWithString:imageURL];
        NSData  *data = [NSData dataWithContentsOfURL:url];
        pvc.imageView.image = [[UIImage alloc] initWithData:data];
        pvc.label.text = [individualPhoto objectForKey:@"id"];
        [scroller addSubview:pvc.view];
        [scrollImageView release];
        //[pvc release];
        scrollWidth += scroller.frame.size.width;
    }

    if (arrayCount > 3) {
        pageControl.numberOfPages=3;
    } else {
    pageControl.numberOfPages=arrayCount;
    }
    pageControl.currentPage=0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み格子的夏天 2024-11-26 06:47:28

如果 UIView 子类需要向其自身上方的控制器发送消息,则一种常见(并且经常推荐?)的方法是让视图子类实现委托协议(指向您要使用的视图控制器的弱链接实例变量) )。当视图初始化时,视图控制器应该将自己设置为视图的委托。

If a UIView subclass needs to message a controller above itself, a common (and often recommended?) way to do this is to have the view subclass implement the delegate protocol (a weak linked instance variable pointing to the view controller that you want to use). The view controller should then set itself as the delegate of the view when that view is being initialize.

爱要勇敢去追 2024-11-26 06:47:28

尝试在 PhotoViewController 中定义此方法:

+ (YOURTABBARCONTROLLER*)parentTabBarController:(UIResponder*)view {
    id nextResponder = nil;
    id v = view;
    while (nextResponder = [v nextResponder]) {
               NSLog(@"Found Responder: %@", nextResponder); //-- ADDED THIS
       if ([nextResponder isKindOfClass:[YOURTABBARCONTROLLER class]])
          return nextResponder;
        v = nextResponder;
    }
    return nil;
 }

它将遍历响应者链并返回找到的给定类型的第一个控制器。将 YOURTABBARCONTROLLER 替换为您实际的标签栏控制器类,您应该能够

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                             self.label.text,
                                             num];

    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;
    // self.tabBarController.selectedIndex = 0;
}

-(IBAction)likeCommentButton:(UIButton *)sender
{
    //code goes here
    TypeSomethingViewController *typeSomethingViewController = [[TypeSomethingViewController alloc] init];
    typeSomethingViewController.delegate = self;

    [self presentModalViewController:typeSomethingViewController animated:YES];
    [typeSomethingViewController release];
}

-(void)typeSomethingViewController:(TypeSomethingViewController *)controller didTypeSomething:(NSString *)text
{
    //NSLog(@"response: %@", controller);

    NSString *commentID = self.label.text;
    for(UIViewController *controller in [PhotoViewController parentTabBarController:self.parentViewController.view].viewControllers)
    {
        if([controller isKindOfClass:[DemoAppViewController class]])
        {
            DemoAppViewController *davc = (DemoAppViewController *)controller;
            //[davc commentPicture:commentID :message];
            [davc likePicture:commentID];
        }
    }
    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;

    [self dismissModalViewControllerAnimated:YES];    
}

Try defining this method in your PhotoViewController:

+ (YOURTABBARCONTROLLER*)parentTabBarController:(UIResponder*)view {
    id nextResponder = nil;
    id v = view;
    while (nextResponder = [v nextResponder]) {
               NSLog(@"Found Responder: %@", nextResponder); //-- ADDED THIS
       if ([nextResponder isKindOfClass:[YOURTABBARCONTROLLER class]])
          return nextResponder;
        v = nextResponder;
    }
    return nil;
 }

it will traverse the responder chain and return the first controller of a given type that is found. Replace YOURTABBARCONTROLLER with your actual tab bar controller class and you should be able to have:

-(IBAction)likeButton:(UIButton *)sender
{
    //this part works
    NSString *num = @"2";
    self.label.text = [NSString stringWithFormat:@"%@ + %@",
                                             self.label.text,
                                             num];

    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;
    // self.tabBarController.selectedIndex = 0;
}

Updated

-(IBAction)likeCommentButton:(UIButton *)sender
{
    //code goes here
    TypeSomethingViewController *typeSomethingViewController = [[TypeSomethingViewController alloc] init];
    typeSomethingViewController.delegate = self;

    [self presentModalViewController:typeSomethingViewController animated:YES];
    [typeSomethingViewController release];
}

-(void)typeSomethingViewController:(TypeSomethingViewController *)controller didTypeSomething:(NSString *)text
{
    //NSLog(@"response: %@", controller);

    NSString *commentID = self.label.text;
    for(UIViewController *controller in [PhotoViewController parentTabBarController:self.parentViewController.view].viewControllers)
    {
        if([controller isKindOfClass:[DemoAppViewController class]])
        {
            DemoAppViewController *davc = (DemoAppViewController *)controller;
            //[davc commentPicture:commentID :message];
            [davc likePicture:commentID];
        }
    }
    [PhotoViewController parentTabBarController:self.view].selectedIndex = 0;

    [self dismissModalViewControllerAnimated:YES];    
}
电影里的梦 2024-11-26 06:47:28

尝试切换到第一个控制器。

self.tabBarController.selectedViewController 
= [self.tabBarController.viewControllers objectAtIndex:0];

更改索引值以切换到您想要切换到的控制器。

Try this out to switch to the first controller.

self.tabBarController.selectedViewController 
= [self.tabBarController.viewControllers objectAtIndex:0];

Change the index value to switch to whichever controller you want to switch to.

聽兲甴掵 2024-11-26 06:47:28

tabBarController 应该是应用程序委托的一个属性。

尝试使用以下方式访问它:

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication]
                                               delegate];
appDelegate.tabBarController.selectedIndex = 0;

除非您在 PhotoViewController 中声明相同的属性,否则您将无法以这种方式访问​​它。

尝试像这样在 PhotoViewController.h 中导入应用程序委托标头:

#import "MyAppDelegate"

然后尝试用上面的代码替换 tabBarControllertbc,如示例所示,这是为此属性指定的名称。

The tabBarController should be a property of the app delegate.

Try accessing it with :

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication]
                                               delegate];
appDelegate.tabBarController.selectedIndex = 0;

Unless you declare the same property in PhotoViewController, you won't be able to access it this way.

Try importing the app delegate header in PhotoViewController.h like so :

#import "MyAppDelegate"

then try this code above replacing tabBarController by tbc which, as the example suggests is the name given to this property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文