【问题标题】:passing the value from one class to other class将值从一个类传递到另一类
【发布时间】:2012-11-28 10:24:58
【问题描述】:

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

我想要做的就是我们在 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..... )

        }

请指导我在哪里做错了。

【问题讨论】:

  • 你能告诉我你使用哪个代码导航到 DetailsOfPeopleViewController

标签: iphone nsstring uilabel


【解决方案1】:

只需在你的代码中做一些事情,你就会得到你的字符串

1 :->

在全球范围内声明您的 DetailsOfPeopleViewController

即在.h

DetailsOfPeopleViewController *detail  

2 :->

在你的 viewDidLoad 中分配它在内存中

detail=[[DetailsOfPeopleViewController alloc] init];  

3 :->

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

@property(nonatomic,copy) NSString *testPersonNotesStr;  

您可以使用 NSUserDefault 发送数据

设置值

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

获取值

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

【讨论】:

  • 要推送到DetailsOfPeopleViewController的代码在哪里,意思是你要使用哪个代码哟DetailsOfPeopleViewController
  • 还有一些其他的类,它的名字是 PeopleHomeViewController,它包含表格视图,如果我们点击 tableView 的行,那么它将转到 DetailsOfPeopleViewController。
  • 哦....我明白了...非常感谢...你能解释一下它没有以我尝试的方式显示...。
  • 根据您的方式,您正在制作 secondView 的对象并在字符串中传递值,并且在 ThirdView(PeopleHomeViewController) 中您再次制作 secondView 的对象并使用该对象进行导航......所以这完全新对象...它没有传递字符串值
  • 现在接受 r8??这是我第一次检查在哪里接受答案...
【解决方案2】:

你让它成为一个属性,所以可以尝试使用 set 方法。此外,您需要在设置值之前初始化 ViewControlelr。所以:

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

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

【讨论】:

    【解决方案3】:

    使用委托传递值use this reference

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 2018-10-09
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多