我使用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
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
我可以轻松获取滑块的值以显示在侧面视图上,但我也希望在主视图上显示该值。
答案 0 :(得分:0)
在礼貌的编程圈子中,全局变量通常是不受欢迎的。
如果您希望MainViewController中的方法对FlipsideViewController中的状态更改做出反应,那么您可以使MVC成为FVC的委托,并在值发生变化时让FVC在MVC上调用方法。
这里有代表设计模式的一个很好的解释:http://mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html