【问题标题】:Passing UISlider Data From a XIB's View Controller to Non-XIB's View Controller With UITabBarController使用 UITabBarController 将 UISlider 数据从 XIB 的视图控制器传递到非 XIB 的视图控制器
【发布时间】:2013-04-06 19:41:30
【问题描述】:

我已经遵循了几个关于在视图控制器之间传递数据的指南,但它们似乎涉及在任何等效的 -(void)goToNextView 方法中设置 secondViewController 的属性。我试图弄清楚如何在使用应用程序委托中创建的标签栏控制器时从第一个视图控制器中为第二个视图控制器中的属性分配值。

当前设置:

AppDelegate.m

@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   ...
    UITabBarController *tbc = [[UITabBarController alloc] init];
    FirstViewController *vc1 = [[FirstViewController alloc] init];
    SecondViewController *vc2 = [[SecondViewController alloc] init];

    [tbc setViewControllers: [NSArray arrayWithObjects: vc1, vc2, nil]];

    [[self window] setRootViewController:tbc];
    [self.window makeKeyAndVisible];
    return YES;
}
...
@end

FirstViewController.h

#import "SecondViewController.h"

@interface FirstViewController : UIViewController
{
    SecondViewController *vc2;

    IBOutlet UISlider *sizeSlider;
}

@property (nonatomic, retain) SecondViewController *vc2;
@property (strong, nonatomic) UISlider *mySlider;

-(IBAction) mySliderAction:(id)sender;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

@synthesize vc2, mySlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:@"xib"]; 
    }
    return self;
}
...
- (IBAction) mySliderAction:(id)sender
{
    NSString *str = [[NSString alloc] initWithFormat:@"%3.2f", mySlider.value];
    if(self.vc2 == nil)
    {
        SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
        self.vc2 = viewTwo;
    }
    vc2.sliderValueString = str;  
}
...
@end

SecondViewController.h

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

@interface SecondViewController : UIViewController
{   
    NSString *sliderValueString; 
}

@property (copy) NSString *sliderValueString;
@end

SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize sliderValueString;

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:@"View 2"];
    }
    return self;
}

-(void) loadView
{
    CGRect frame = [[UIScreen mainScreen] bounds];
    myView *view = [[myView alloc] initWithFrame:frame];

    printf("Slider Value: %s", [sliderValueString UTF8String]);

    [self setView: view];
}
...
@end

myView.h 和 .m 可能与问题无关,但我有 .h 子类 UIView,并且 .m 使用 -(id)initWithFrame:(CGRect)frame 创建视图

我猜我将第二个 VC 的属性分配在错误的位置 (mySliderAction),因为第二个 VC 中的 printf() 是空白的,所以我的问题是:应该在哪里分配属性,或者我做错了什么阻止第二个 VC 允许它的 sliderValueString 不被分配(或保持)?

谢谢!

【问题讨论】:

    标签: ios delegates uitabbarcontroller


    【解决方案1】:

    您可以在mySliderAction 中设置属性,但问题是您将其设置在错误的对象上。

    在 App Delegate 中,您为 tabControllers 的第 2 项创建 viewController:

    SecondViewController *vc2 = [[SecondViewController alloc] init];
    

    在vc1的mySliderAction创建一个vc2的实例:

    SecondViewController *viewTwo = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    

    这不是您通过 tabController 上的第二个选项卡导航到的实例,而是您正在设置的 sliderValueString 属性的实例。

    你需要引用已经存在的实例,而不是在这里创建一个新实例:

     SecondViewController *viewTwo = [self.tabBarController.viewControllers objectAtIndex:1]
    

    那你应该没事。

    【讨论】:

    • 谢谢!解决了问题!
    猜你喜欢
    • 2019-07-03
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多