在IOS实用程序应用程序视图之间共享数据



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

我已经在全局变量上搜索了一个小时,但找不到一个适用于我的具体案例的简单答案。

请帮助

(注意:我对IOS和objective 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

MainViewController.h

#import "FlipsideViewController.h"
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
- (IBAction)showInfo:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *sliderValueOnMain;
@end

MainViewController.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

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新