如何使用自己的委托方法添加子视图?

时间:2012-06-09 11:54:02

标签: iphone ios ipad delegates addsubview

enter image description here

当我的屏幕截图显示我在SliderValueViewController.xib.i上有UIButton时想要当我点击这个UIButton时它会自动在Secondview.xib中添加子视图(名称是detailview)。这是我的代码。在SliderValueViewController.h中,我声明了我的协议,

#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;

这是我的Button功能..

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

现在我的secondview.h

enter image description here

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

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

这是我想要的功能......

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

在ELSLiderl.m中,我这样做..

 -(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)调用,但它不是调用委托方法方法( - (void)displayview)在第二个视图中。任何人都可以提前帮助我做出错误。

1 个答案:

答案 0 :(得分: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];来电时,会在displayview

中拨打secondview