在我的应用程序中,当用户按下按钮时,我想在不同的视图上生成有关该主题的信息。 E.G当用户按下cat按钮时,我想要一个新的视图来显示标题和描述。然而,当他们按下它时,它会转到相同的视图,但它会改变信息。
firstViewController.h
#import "secondview.h"
@interface ViewController :UIViewController
{
secondview *secondviewData;
IBOutlet UITextField *textfield;
}
@property (nonatomic, retain)secondview*secondviewData;
-(IBAction)passdata:(id)sender;
@end
firstViewController.m
#import "ViewController.h"
#import "secondview.h"
@implementation ViewController
@synthesize secondviewData;
-(IBAction)passdata:(id)sender
{
secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil];
self.secondviewData = second;
secondviewData.passedValue = @"dog Info";
[self presentModalViewController:second animated:YES];
}
@end
secondViewController.h
@interface secondview :UIViewController
{
IBOutlet UILabel *label;
NSString *passedValue;
}
@property (nonatomic, retain)NSString *passedValue;
-(IBAction)back:(id)sender;
@end
SecondViewController.m
#import "ViewController.h"
@implementation secondview
@synthesize passedValue;
-(IBAction)back:(id)sender
{
ViewController *second = [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
- (void)viewDidLoad
{
label.text = passedValue;
[super viewDidLoad];
}
@end
当我执行此代码时,会出现SIGABRT
答案 0 :(得分:1)
如果您的下一个视图是从当前视图中进行的,则可以使用:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"nextViewSegue"]){
nextViewController *nextViewObj = segue.destinationViewController;
nextViewObj.someNSString = @"the string you wanna to pass";
}
else if ([segue.identifier isEqualToString:@"nextViewSegue2"]){
otherNextViewController *otherNextViewObj = segue.destinationViewController;
otherNextViewObj.someNSString = @"the string you wanna to pass";
}
}
您可以使用全局变量传递参数。
创建一个类
·H
@interface iXxxxxxGlobal : NSObject
{
NSString *globalString;
}
@property (nonatomic,retain)NSString *globalString;
+(iXxxxxxGlobal *)getInstance;
-(void) updateSetting;
@end
的.m
#import "iXxxxxxGlobal.h"
@implementation iXxxxxxGlobal
@synthesize globalString;
static iXxxxxxGlobal *instance = nil;
+(iXxxxxxGlobal *)getInstance
{
if(instance ==nil)
{
instance = [iXxxxxxGlobal new];
// Get initial value from Preferences and Settings.
// Use <<Preferences and Settings>>
}
return instance;
}
-(void)updateSetting
{
//Do something to update <<Preferences and Settings>>
}
@end
每次使用全局变量时,只需要这样做:
iXxxxxxGlobal *iXxxxxxGlobalObj=[iXxxxxxGlobal getInstance];
iXxxxxxGlobalObj.globalString = @"your string";
NSString *localString = iXxxxxxGlobalObj.globalString;