我使用 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
我可以轻松地将滑块的值显示在翻转视图上,但是我也希望该值显示在主视图上。