0

在我的应用程序中,当用户按下按钮时,我想在不同的视图上生成有关该主题的信息。例如,当用户按下 cat 按钮时,我想要一个新视图来显示标题和描述。但是,当他们按下 dog 时,它会转到相同的视图,但会更改信息。

第一视图控制器.h

#import "secondview.h"

@interface ViewController :UIViewController 
{
    secondview *secondviewData;
    IBOutlet UITextField *textfield;
}

@property (nonatomic, retain)secondview*secondviewData;

-(IBAction)passdata:(id)sender; 

@end

第一视图控制器.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

第二视图控制器.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

4

1 回答 1

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;
于 2013-04-09T06:39:53.697 回答