1

我使用 Xcode 4.2 中的 Utility Application 模板创建了一个非常基本的应用程序,其中我希望从 MainViewController 中可用的 FlipsideViewController 中的滑块或文本字段获取整数变量。

我一直在寻找关于全局变量的小时,但找不到适合我的具体情况的简单好答案。

请帮忙

(注意:我在为 IOS 和目标 C 开发方面非常新。尽你所能为我降低你的答案!)

非常感谢

以下是我的代码的基本版本,显示了我想要做什么。

FlipSideViewController.h

#import <UIKit/UIKit.h>

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

@interface FlipsideViewController : UIViewController

@property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;

- (IBAction)done:(id)sender;
- (IBAction)sliderChange:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *sliderValueOnFlipside;

@end

FlipSideViewController.m

#import "FlipsideViewController.h"

@interface FlipsideViewController ()

@end

@implementation FlipsideViewController
@synthesize sliderValueOnFlipside;

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidUnload
{
    [self setSliderValueOnFlipside:nil];
    [super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Actions

- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

- (IBAction)sliderChange:(id)sender {
    UISlider *slider = (UISlider *) sender;
    int var = [slider value];

    sliderValueOnFlipside.text = [NSString stringWithFormat:@"Slider Value is %d",var];
}

@end

主视图控制器.h

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

- (IBAction)showInfo:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *sliderValueOnMain;

@end

主视图控制器.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController
@synthesize sliderValueOnMain;

- (void)viewDidLoad
{
    [super viewDidLoad];



    sliderValueOnMain.text = [NSString stringWithFormat:@"Slider Value from flipside is %d", var];
}

- (void)viewDidUnload
{
    [self setSliderValueOnMain:nil];
    [super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
}

@end

我可以轻松地将滑块的值显示在翻转视图上,但是我也希望该值显示在主视图上。

4

1 回答 1

0

全局变量在礼貌的编程圈子中通常不受欢迎。

如果您希望 MainViewController 中的方法对 FlipsideViewController 中的状态变化做出反应,那么您可以使 MVC 成为 FVC 的委托,并让 FVC 在值更改时调用 MVC 上的方法。

这里对委托设计模式有很好的解释:http: //mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

于 2012-09-11T18:22:35.770 回答