【问题标题】:Using protocol to trigger a segue from a UIView inside a UIViewcontroller使用协议从 UIViewcontroller 中的 UIView 触发 segue
【发布时间】:2014-01-18 05:10:49
【问题描述】:

配置:

-UIviewController 嵌入在Navigationcontroller 中。 -UIviewController 有一个 UIscrollview 作为子视图 -UIscrollview 有一些创建图表的视图:每个包含图表的视图都有自己的 .h 和 .m 文件,我想从这个文件触发一个到 tableview 控制器的 segue。 -A Tableviewcontroller 已添加到 xcode 中,并且还创建了从 UIviewControllerTableViewcontroller 的 segue (Xcode)

-在UIView 中创建了一个协议,以便从那里推送segue。

问题:

delegate 总是 nil,segue 方法永远不会被调用

UIVIEW .h 文件

@protocol UItoUIvcDelegate <NSObject>

-(void)triggerSegue;


@end

@interface CFfirstGraph : UIView <CPTPlotDataSource , CPTPieChartDelegate,UIActionSheetDelegate>

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


@end

UIVIEW .m 文件 (sn-p)

-(void)pieChart:(CPTPieChart *)pieChart sliceWasSelectedAtRecordIndex:(NSUInteger)index
 {

     if (self.delegate == nil)
     {
         NSLog(@"nil");
     }
     [self.delegate triggerSegue];
}

UIVIEWCONTROLLER .h 文件

#import "CFfirstGraph.h"

@interface CFMainViewController : UIViewController <UItoUIvcDelegate, UITableViewDelegate, UITableViewDataSource>


@end

UIVIEWCONTROLLER .m 文件 (sn-p)

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.contentSize = CGSizeMake(320, 1000);
    [self.view addSubview:self.scrollView];
    CFfirstGraph *click =[[CFfirstGraph alloc]init];
    click.delegate = self ;

}


-(void)triggerSegue

{
    [self performSegueWithIdentifier:@"detailedData" sender:self];
    NSLog(@"estoy aqui");
}

我做错了什么?为什么代表总是 nil ?我尝试添加方法 setDelegate 但仍然没有运气。

谢谢, dom

【问题讨论】:

    标签: objective-c uiview uiviewcontroller delegates protocols


    【解决方案1】:

    CFfirstGraph 设为强属性。

    @property (strong, nonatomic) CFfirstGraph * click;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.click =[[CFfirstGraph alloc]init];
        self.click.delegate = self ;
    
    
        self.scrollView.contentSize = CGSizeMake(320, 1000);
        [self.view addSubview:self.scrollView];
    
    }
    

    【讨论】:

    • 不,仍然为零,但感谢您的回答。我试图在 viewDidAppear 中移动 click 和 click.delegate 行,但它也不起作用。我认为该协议已正确实现,因此问题必须隐藏在滚动视图和带有图表的子视图之间的某个位置。
    • 能否将源代码发送至 shamsutk87@gmail.com ?
    【解决方案2】:

    好的,经过几个小时的汗水我发现了问题。

    首先...delegate = nil 不是主要问题。

    真正的问题是触发 segue 的协议方法从未被调用过。

    如果我创建并初始化一个 CFfirstGraph 对象(甚至是属性),它将与已在 x 代码中创建的视图无关,这是主要问题。

    另一方面...如果我“CTRL-拖动”一个从 UIview 到 CFMainViewController 的出口,我将拥有一个正是我需要的属性:

    @interface CFMainViewController () <UIScrollViewDelegate>
    @property (weak, nonatomic) IBOutlet CFfirstGraph *FirstGraph;
    

    然后我在 viewDidLoad 中将委托分配给自己(CFMainViewController):

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.FirstGraph.delegate = self ;
    
        }
    

    并且委托方法“triggerSegue”将从 UIVIEW 调用执行。

    最好的问候, dom

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多