将值从一个类传递给其他类

时间:2012-11-28 10:24:59

标签: iphone nsstring uilabel

我在AddNotesViewController的标签中显示DetailsOfPeopleViewController类字符串的值时遇到问题。

我想要做的就是我们在1班的UITextView中输入的内容,应该显示在另一个班级的UILabel中。

在档案AddNotesViewController.h

   UITextView *txtView;

  @property(nonatomic,retain)IBOutlet     UITextView *txtView;
文件AddNotesViewController.m

中的

 @synthesize txtView;

此后当我点击标签栏项“保存”时,它应该保存在数据库中, 它保存到数据库并显示值,但它没有将值传递给

DetailsOfPeopleViewController

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

        NSLog(@"Tabbar selected itm %d",item.tag);

       switch (item.tag) {
          case 0:

         appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];


        NotesTable *crashNotInvolvingObj11 = (NotesTable *)[NSEntityDescription       
    insertNewObjectForEntityForName:@"NotesTable" inManagedObjectContext:[appDelegate    managedObjectContext]];

        // CameraViewController *camera=[[CameraViewController alloc] init];
            crashNotInvolvingObj11.personNote=[NSString    stringWithFormat:@"%@",txtView.text];

        DetailsOfPeopleViewController *detail=[DetailsOfPeopleViewController alloc];
        detail.testPersonNotesStr  =[NSString stringWithFormat:@"%@",txtView.text];
        //(value of testPersonNotesStr is DISPLYING here... )
        [appDelegate saveContext];
        [detail release];

        break;
        ..................

     }
DetailsOfPeopleViewController.h

中的

    NSString *testPersonNotesStr;
    UILabel *PersonNotesLbl;

     @property(nonatomic,retain)IBOutlet UILabel *PersonNotesLbl;
     @property(nonatomic,retain) NSString *testPersonNotesStr;
DetailsOfPeopleViewController.m

中的

       @synthesize PersonNotesLbl;
       @synthesize  testPersonNotesStr;



      - (void)viewDidLoad
      {
         [super viewDidLoad];
            [PersonNotesLbl setText:testPersonNotesStr];

         NSLog(@"PERSON NOTE is........ %@",testPersonNotesStr);
           //(value of testPersonNotesStr is NOT DISPLYING here..... )

        }

请指导我在哪里犯错误。

3 个答案:

答案 0 :(得分:0)

您将其设为属性,因此可以尝试使用set方法。此外,您需要在设置值之前初始化ViewControlelr。所以:

DetailsOfPeopleViewController *detail=[[DetailsOfPeopleViewController alloc] init];
[detail setTestPersonNotesStr:[NSString stringWithFormat:@"%@",txtView.text]];

我没有看到您实际显示此视图的位置,您必须显示此视图的相同实例才能实际看到该值。

答案 1 :(得分:0)

使用委托来传递值use this reference

答案 2 :(得分:0)

只需在代码中执行一些操作即可获得字符串

1: - >

全局声明您的 DetailsOfPeopleViewController

即。在.h

DetailsOfPeopleViewController *detail  

2: - >

viewDidLoad中的

将其分配到内存

detail=[[DetailsOfPeopleViewController alloc] init];  

3: - >

只需声明要复制的字符串属性

@property(nonatomic,copy) NSString *testPersonNotesStr;  

您可以使用NSUserDefault

发送数据

SetValue

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSString stringWithFormat:@"%@",txtView.text]; forKey:@"testPersonNotesStr"];
[defaults synchronize];

GetValue

testPersonNotesStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"testPersonNotesStr"];
NSLog(@"PERSON NOTE is........ %@",testPersonNotesStr);  

按照我的回答i have NSString value in addViewController and i want to display this value in UILabel of DetailsViewController