iphone - 委托(delegate)无法正常工作
<p><p>我正在尝试使用委托(delegate)在导航堆栈上的 subview 和父 View 之间传递一些信息。</p>
<p>但是由于某种原因,当我从 subview 执行委托(delegate)然后从导航 Controller 弹出 subview 时,它永远不会进入在父 View 中设置的委托(delegate)方法。我已经尝试过 NSLogs & Breakpoints 线程是挑衅没有在主视图中使用这个委托(delegate)方法,所以我希望你能帮助我。</p>
<p>首先,我将向您展示我是如何设置我的代理的,我在 subview 中调用它,然后我在主视图中设置它,希望你们能够看到我到目前为止还没有看到的东西。 </p>
<p>Subview.h//我省略了与委托(delegate)无关的内容</p>
<pre><code>#import <UIKit/UIKit.h>
//Delegate - Protocol stuff for passing data from this view to the parent view
@protocol PassSearchData <NSObject>
@required
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath;
@end
@interface SecondViewController : UITableViewController <UITableViewDataSource> {
//Delegate (Used to pass information back to parent view)
id <PassSearchData> delegate;
}
//Delegate (Used to pass information back to parent view)
@property (strong) id delegate;
@end
</code></pre>
<p>SecondView.m</p>
<pre><code>#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
//..
//Delegate (Used to pass information back to parent view)
@synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Access selected cells content (cell.textLabel.text)
UITableViewCell *cell = ;
//Parent view logic (sends info back to the correct cell in parent view)
if (parentViewSelectedIndexPath.section == 0)
{
if (parentViewSelectedIndexPath.row == 0)
{
//Predicates restrict the values that will be returned from the query
NSPredicate *predicate = ;
filterArray = ;
[setManufactureSearchFields:filterArray withIndexPath:indexPath];
// NSLog(@"Filtered Array = %@", filterArray);
}
}
; //pops current view from the navigatoin stack
}//...
</code></pre>
<p>这就是我在 FirstViewController 中设置它的方式</p>
<p>FirstViewController.h</p>
<pre><code>#import <UIKit/UIKit.h>
#import "VehicleResultViewController.h" //With this included I can now use the PassSearchData delegates of this view for passing data
@interface VehicleSearchViewController : UITableViewController <PassSearchData> {
//..
</code></pre>
<p>FirstViewController.m</p>
<pre><code>#import "VehicleSearchViewController.h"
#import "VehicleResultViewController.h" //Subview
//..
#pragma mark - Received data from Sub view delegates
//These are the delegate method for passing data from the child to the parent view (parent being this view, with the delegate being declared in the subview)
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
manufactureSearchObjectString = [ objectAtIndex:0];
manufactureIdString = [ objectAtIndex:0]; //Restricts Models dataset
manufactureResultIndexPath = myIndexPath;
}
</code></pre>
<p>如果有人知道我遗漏了什么/做错了什么,那么任何帮助都会非常有帮助</p>
<p>如有任何问题,请告诉我。</p>
<p><strong>解决方案</strong></p>
<p>在firstviewcontroller里面的时候我去加载secondviewcontroller里面
tableView:didSelectRowAtIndexPath: 在将 View 推送到导航堆栈之前,我忘记设置 secondviewcontrollers 委托(delegate)。所以缺少的代码是这样的。</p>
<p>FirstView.m</p>
<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the subview ready for use
VehicleResultViewController *vehicleResultViewController = [ initWithNibName:@"VehicleResultViewController" bundle:nil];
//Pass the selected object to the new view controller.
;
;
//etc
</code></pre>
<p>谢谢大家。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>好的,首先,您的委托(delegate)应该是一个弱引用以避免保留循环。但我怀疑委托(delegate)可能没有准备好。您能否显示实例化 SecondViewController 并设置委托(delegate)的代码?您可以在委托(delegate)调用处设置断点并验证它不是零吗? </p></p>
<p style="font-size: 20px;">关于iphone - 委托(delegate)无法正常工作,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8783269/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8783269/
</a>
</p>
页:
[1]