0

在此处输入图像描述

正如我的屏幕截图所示,我在 SliderValueViewController.xib 上有 UIButton。我希望当我单击此 UIButton 时,它会自动在 Secondview.xib 中添加子视图(名称为 detailview)。这是我的代码。在 SliderValueViewController.hi 中声明我的协议,

#import <UIKit/UIKit.h>
@protocol Testmyview

-(void)displayview;

@end


@interface SliderValueViewController : UIViewController {

    IBOutlet UILabel *sliderValue;
    IBOutlet UIButton *btngo;
        id <Testmyview> delegate;
}

@property (nonatomic, retain) IBOutlet UILabel *sliderValue;
@property (nonatomic, assign)id <Testmyview> delegate;

-(void)updateSliderValueTo:(CGFloat)_value;
-(IBAction)gotodetailview;
@end

在 SliderValueViewController.m

           #import "SliderValueViewController.h"
           #import"secondview.h"
           @implementation SliderValueViewController
           @synthesize sliderValue;
           @synthesize delegate;

这是我的按钮功能..

    -(IBAction)gotodetailview
      {
         [self.delegate displayview];
      }

现在我的 secondview.h

在此处输入图像描述

而我的 secondview.m 看起来就像这样..

   #import "secondview.h"
   #import "SliderValueViewController.h"
   #import "ELCSlider.h"
   @implementation secondview

这是我想要的功能..

  -(void)displayview
    {
      [self.view addSubview:detailview];    
   }

在 ELSLiderl.mi 中执行此操作..

 -(id)initWithCoder:(NSCoder *)aDecoder {

 if(self = [super initWithCoder:aDecoder]) {
    [self addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
    sliderValueController = [[SliderValueViewController alloc] initWithNibName:@"SliderValueViewController" bundle:[NSBundle mainBundle]];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:sliderValueController];
    [popoverController   setPopoverContentSize:sliderValueController.view.frame.size];
     }

    return self;    
   }

 -(void)valueChanged {

[sliderValueController updateSliderValueTo:self.value];

CGFloat sliderMin =  self.minimumValue;
CGFloat sliderMax = self.maximumValue;
CGFloat sliderMaxMinDiff = sliderMax - sliderMin;
CGFloat sliderValue = self.value;

if(sliderMin < 0.0) {

    sliderValue = self.value-sliderMin;
    sliderMax = sliderMax - sliderMin;
    sliderMin = 0.0;
    sliderMaxMinDiff = sliderMax - sliderMin;
}

CGFloat xCoord = ((sliderValue-sliderMin)/sliderMaxMinDiff)*[self frame].size.width-sliderValueController.view.frame.size.width/2.0;

CGFloat halfMax = (sliderMax+sliderMin)/2.0;

if(sliderValue > halfMax) {

    sliderValue = (sliderValue - halfMax)+(sliderMin*1.0);
    sliderValue = sliderValue/halfMax;
    sliderValue = sliderValue*11.0;

    xCoord = xCoord - sliderValue;
}

else if(sliderValue <  halfMax) {

    sliderValue = (halfMax - sliderValue)+(sliderMin*1.0);
    sliderValue = sliderValue/halfMax;
    sliderValue = sliderValue*11.0;

    xCoord = xCoord + sliderValue;
}

[popoverController presentPopoverFromRect:CGRectMake(xCoord, 0, sliderValueController.view.frame.size.width, sliderValueController.view.frame.size.height) inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
 }

使用所有这些代码,它不会给我带来任何磨损或错误,但它没有添加子视图。当我使用断点时,我的 UIButton 方法(-(IBAction)gotodetailview)被调用,但它没有在 secondview 中调用委托方法方法(-(void)displayview)。任何人都可以提前帮助我犯什么错误.thanx。

4

1 回答 1

2

secondview.m

您似乎忘记设置的delegate属性SliderValueViewController

在呈现或推送“ SliderValueViewController”的功能中你会有这样的东西

SliderValueViewController *controller = [[SliderValueViewController alloc] init];
//You will need to add this code
//self is secondview
controller.delegate = self;

现在当接到电话时[self.delegate displayview];,这将调用displayviewsecondview

于 2012-06-09T13:26:03.460 回答