2013-03-26 80 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

相關問題