jjzjj

ios - 在 2 个 subview Controller 之间来回传递数据

coder 2024-01-21 原文

想看看是否有人可以就如何解决我目前面临的 iPad 应用程序问题提供一些建议/指示。这是我在下面描述的图形的链接 http://www.yogile.com/dsruyzk7/41m/share/?vt=QANtu4j

基本上我有一个包含 2 个 subview Controller 的 parentViewController。 child 1 有一个 UITableView 和 child 2 有一个自定义的 UIView。我能够从 didSelectRowAtIndexPath 将信息从 Child 1 的 UITableview 加载到 Child 2 的自定义 UIView。数据是之后 显示在 Child 2 上 我做了一些处理。处理完成后,我需要更新 Child 1 上的 UITableView,以便新的/更新的数据 显示在 UITableView 上。我尝试在 Child 1 上创建一个委托(delegate)但没有工作,也许我设置了错误。因此,我们将不胜感激任何帮助建议。

谢谢

.h 在 Child 2 ViewController 上

@class Child2ViewController;

@protocol child2Delegate <NSObject>
- (void)refreshTable:(Child2ViewController*)controller passedDict:(NSDictionary *)dict;

@interface Child2ViewController:UIViewController<UITableViewDataSource, UITableViewDelegate> {
    UITableView *myTableView;
    id<child2Delegate> delegate;
    Child1ViewController *child1VC;
}

@property (nonatomic, weak) id<child2Delegate> delegate;
…
…

@end

.m 在 Child 2 ViewController 上

@synthesize delegate;
…
..
..
#after all the processing is done we are ready to refresh the view
#updatedDictForTableView is basically a NSDictionary and has the updated data needed
#for the UITableview on child1VC.

-(void)processData {
    child1VC.delegate = self
    NSLog(@"Dump the updated Data : %@", updatedDictForTableView);
    [delegate refreshTable:self passedDict:updatedDictForTableView;
}

.h 在 Child1 View Controller 中

@interface Child1ViewController : UIViewController <child2Delegate> {
    ….
    ….
    ..
}

.m 在 Child1 View Controller 中

- (void)refreshTable:(Child2ViewController*)controller passedDict:(NSDictionary *)dict {
    NSLog(@"dump dict %@", dict);
    [myTableView reloadData];

}

最佳答案

你可以在没有委托(delegate)的情况下做到这一点,使用 NSNotificationcenter 发送一个通知,其中包含你在 nsdictionary: exple 中的值:

  // in child1 ( in viewDiDLoad function )
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FunctionToReloadData:) name:@"ProcessDone" object:nil];

  // remove the listnerin child1 ( in viDidUnload )
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"ProcessDone"];

 //in child2 ( in the end of your processData function )
 [[NSNotificationCenter defaultCenter] postNotificationName:@"ProcessDone" object:nil userInfo:updatedDictForTableView ];


 //in child1
 -(void) FunctionToReloadData:(NSNotification *)notification
 {
    // get the sended dictionary
    NSDictionary *tmpDic = [notification userInfo];
     .
     .
     [tableView reloadData];

 }

关于ios - 在 2 个 subview Controller 之间来回传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12416971/

有关ios - 在 2 个 subview Controller 之间来回传递数据的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  3. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  6. ruby-on-rails - `a ||= b` 和 `a = b if a.nil 之间的区别? - 2

    我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行

  7. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  8. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  9. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  10. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

随机推荐