2013-03-26 88 views
1

我是新来的ios和Objective-C。请对我有点耐心。UIScrollView以编程方式添加按钮并滚动

在我的应用程序中,我试图开发的功能是一个小的滚动工具栏,它包含几个按钮,状态切换按钮。工具栏上方还有另一个按钮,用于滑入和滑出工具栏。我成功实现的第二个功能,使用整个设置的视图,并且当幻灯片按钮触摸事件动画整个视图上下。

对于scrollview,我想以编程方式添加按钮,因此我画了一个插座,并尝试添加一个自创的按钮作为子视图。但是我看不到按钮。我也不能观察滚动视图滑动。

下面是我添加的按钮的方式: 另外,我对上述功能总的看法是非常少的大小比总视图控制器

UIButton *button = [[UIButton alloc] init]; 

button.titleLabel.text = @"hello"; 

[_scrollTop setContentSize:self.view.frame.size]; 

[_scrollTop addSubview:button]; 

两个图像下来,我希望,应该可以帮助您了解我正在尝试开发的功能。如果有人知道存在类似的功能,请指导我。

Closed

Opened

编辑:代码完全是

// 
// HCILocationViewController.m 
// app2 
// 
// Created by Ravi Vooda on 13/03/13. 
// Copyright (c) 2013 housing.co.in. All rights reserved. 
// 

#import "HCILocationViewController.h" 
#import "HCIAnnotationViewController.h" 

@interface HCILocationViewController() 

@property int widMap; 
@property BOOL showExploreOptions; 


@end 

@implementation HCILocationViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    _mapLocationView.delegate = self; 
    MKPointAnnotation *annotation = [[HCIAnnotationViewController alloc] 
            initwithHouse:[self singleHome]]; 
    [_mapLocationView addAnnotation:annotation]; 
    _widMap = 10000; 

    _showExploreOptions = NO; 

    /* 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button addTarget:self 
       action:@selector(aMethod:) 
    forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"Show View" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);//give values whatever you want. 
    [_scrollTop addSubview:button]; 
    */ 
    [self addButtonsToScrollView]; 

} 

- (void)addButtonsToScrollView 
{ 
    NSInteger buttonCount = 4; 

    CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f); 

    for (int index = 0; index <buttonCount; index++) { 
     UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [button setTag:index+1]; 

     NSString *title = [NSString stringWithFormat:@"Button %d",index+1]; 
     [button setTitle:title forState:UIControlStateNormal]; 

     [self.scrollTop addSubview:button]; 
     buttonFrame.origin.y+=buttonFrame.size.height+5.0f; 
    } 

    CGSize contentSize = self.scrollTop.frame.size; 
    contentSize.height = buttonFrame.origin.y; 
    [self.scrollTop setContentSize:contentSize]; 

} 

- (void)buttonPressed:(UIButton *)button 
{ 
    NSLog(@"button pressed"); 

    switch (button.tag) { 
     case 1: 
      //Do something 
      break; 

     default: 
      break; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    NSString *identifier = @"currDetailsIdentifier"; 
    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[_mapLocationView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] 
          initWithAnnotation:annotation 
          reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
    return annotationView; 
} 

-(void) mapViewDidFinishLoadingMap:(MKMapView *)mapView 
{ 

    MKMapRect zoomRect = MKMapRectNull; 
    for (id <MKAnnotation> annotation in mapView.annotations) { 
     MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate); 
     MKMapRect pointRect = MKMapRectMake(
              annotationPoint.x - (_widMap/2), 
              annotationPoint.y - (_widMap/2), 
              _widMap, 
              _widMap); 
     if (MKMapRectIsNull(zoomRect)) { 
      zoomRect = pointRect; 
     } else { 
      zoomRect = MKMapRectUnion(zoomRect, pointRect); 
     } 
    } 
    [mapView setVisibleMapRect:zoomRect animated:YES]; 
} 

-(void) setMyHouse:(NSDictionary *)singleHouse { 
    self.singleHome = singleHouse; 
} 


- (IBAction)toggleExploreOptionsView:(UIButton *)sender { 

    sender.selected = !sender.selected; 

    if (_showExploreOptions){ 
     NSLog(@"Close Options"); 
     [UIView animateWithDuration:0.2 animations:^{ 
      _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y + _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height + _exploreOptionsView.frame.size.height); 
     }]; 
    } 
    else { 
     NSLog(@"Open Options"); 
     [UIView animateWithDuration:0.2 animations:^{ 
      _exploreView.frame = CGRectMake(_exploreView.frame.origin.x, _exploreView.frame.origin.y - _exploreOptionsView.frame.size.height, _exploreView.frame.size.width, _exploreView.frame.size.height - _exploreOptionsView.frame.size.height); 
     }]; 
    } 
    _showExploreOptions = !_showExploreOptions; 
} 
@end 
+0

一次在这里发表您的代码。 – Balu 2013-03-26 11:13:50

+0

一旦检查一个,可能与scrollview内容大小有关。 – Balu 2013-03-26 11:34:42

回答

0

如果我理解正确你的问题,你需要有鉴于这应该出现了,点击一个按钮事件崩溃。当显示视图时,它应该有一个可滚动的按钮列表。你正在寻求帮助,以显示许多按钮scrollView。

Demo Source Code

- (void)addButtonsToScrollView 
{ 
    NSInteger buttonCount = 5; 

    CGRect buttonFrame = CGRectMake(5.0f, 5.0f, self.scrollTop.frame.size.width-10.0f, 40.0f); 

    for (int index = 0; index <buttonCount; index++) { 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [button setFrame:buttonFrame]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [button setTag:index+1]; 

     NSString *title = [NSString stringWithFormat:@"Button %d",index+1]; 
     [button setTitle:title forState:UIControlStateNormal]; 

     [self.scrollTop addSubview:button]; 
     buttonFrame.origin.y+=buttonFrame.size.height+5.0f; 
    } 

    CGSize contentSize = self.scrollTop.frame.size; 
    contentSize.height = buttonFrame.origin.y; 
    [self.scrollTop setContentSize:contentSize]; 

} 

- (void)buttonPressed:(UIButton *)button 
{ 
    switch (button.tag) { 
     case 1: 
      //Do something 
      break; 

     default: 
      break; 
    } 
} 
+0

仍然无法使用。是的,这就是我想要开发的功能 – 2013-03-26 11:04:20

+0

@RaviVooda显示为灰色区域的视图是您想要放置scrollView仪表的位置。请确保您的scrollView连接到IBOutlet。您可能需要调整按钮标题的方式,否则它将正常工作 – Anupdas 2013-03-26 11:10:57

+0

是的,它们已连接。我甚至尝试机智不同的名字,仍然不工作......甚至没有按钮显示 – 2013-03-26 11:20:07

相关问题