【问题标题】:How to transfer data ViewControllers/classes using NSNotificationCenter?如何使用 NSNotificationCenter 传输数据 ViewControllers/类?
【发布时间】:2013-06-24 03:26:19
【问题描述】:

我有一个表格视图控制器,它应该填充来自封装在 store 类中的数组的数据。该表需要通过table:numberOfRowsInSection: 方法知道每个部分中有多少行。在此方法中,我需要返回 store 实例中的数组大小。我最初通过将store 设为单例来做到这一点,但被告知这是低效的,使用 NSNotificationCenter 会更好。

据我所知,NSNotificationCenter 所做的只是在另一个对象发布特定通知时触发某些对象中的方法。如何使用 NSNotificationCenter 将数组的大小发送到我的表视图控制器?

【问题讨论】:

  • 如果没有关于数据是什么的信息,就很难确定,但NSNotificationCenter 或单例都不是很好的模式。为什么不向表视图控制器添加一个指向store 的属性?你能多谈谈数据是什么吗?
  • 通过通知发送行数不是一个好主意。通知,顾名思义,应该只用于通知。在您的情况下,它应该用于通知商店发生了一些更改。然后从存储实例(它是单例或视图控制器引用的实例)中检索真实数据(例如,行数、节数等)。
  • 我有一个表格视图控制器,它需要填充一个充满字符串的数组。这个数组是另一个类Store 的属性。我应该在我的表视图控制器启动时创建这个商店并给表视图控制器一个对它的引用吗?
  • 更新:我尝试了在表启动时为表视图控制器提供指向存储的属性的方法。它似乎工作正常,但是否建议使用单例?

标签: ios objective-c nsnotificationcenter


【解决方案1】:

你可以这样做:

...
// Send 
[[NSNotificationCenter defaultCenter] postNotificationName: SizeOfRrrayNotification
                                                    object: [NSNumber numberWithInteger: [array count]]];

...
// Subscribe
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(sizeOfArray:)
                                             name: SizeOfRrrayNotification
                                           object: nil];

// Get size
- (void) sizeOfArray: (NSNotification*) notification
{
    NSNumber* sizeOfArray = (NSNumber*) notification.object;
    NSLog(@"size of array=%i",  [sizeOfArray integerValue]);
}

【讨论】:

    【解决方案2】:

    发布通知:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyArraySize" object: [NSNumber numberWithInteger: [myArray count]]] userInfo:nil];
    

    获取通知:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSizeOfArray:) name:@"MyArraySize" object:nil];
    

    在您收到通知的 viewController 中添加此方法:

    - (void) getSizeOfArray: (NSNotification*) notification
    {
        NSNumber* myArraySize = (NSNumber*) notification.object;
    }
    

    您甚至可以通过“userInfo”发送更多数据,并使用 notification.userInfo 在选择器方法中获取该数据,但请记住它的类型是“NSDictionary

    希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      你计算数组大小的方法:

      -(void)sizeOfArray{
         int size = [myArray count];
         NSMutableString *myString = [NSMutable string];
         NSString *str = [NSString stringwithFormat:@"%d",size];
         [myString apprndString:str];
      
      //It is to be noted that NSNotification always uses NSobject hence the creation of mystring,instead of just passing size
      
         [[NSNotificationCenter defaultCenter] postNotificationName:@"GetSizeOfArray"      object:myString];
      }
      

      现在,一旦您发布了通知,请将其添加到您发送数据的控制器的 viewDidLoad 方法中

      -(void)viewDidLoad{
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(get_notified:) name:@"GetSizeOfArray"  object:nil];
      
      //The selector method should always have a notification object as its argument
      
       [super viewDidLoad];
      
      }
      
      
      - (void)get_notified:(NSNotification *)notif {
          //This method has notification object in its argument
      
          if([[notif name] isEqualToString:@"GetSizeOfArray"]){
             NSString *temp = [notif object];
             int size = [temp int value];
      
             //Data is passed.
              [self.tableView reloadData];  //If its necessary 
      
           }    
      }
      

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2011-12-15
        • 2017-12-01
        • 2016-08-22
        • 2019-10-20
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        相关资源
        最近更新 更多