需要帮助创建iPhone应用程序

时间:2013-05-27 01:28:21

标签: ios software-design

这就是我想要做的: 使用2个选择器视图,您可以选择。 然后,人员点击按钮,从用户选择的内容开始处理。 拾取器使用两个参数,然后数据显示在底部。

我了解C ++并使用菜单样式创建了这个ddos版本,但我对Objective-C和iphone开发非常新。我只是需要把对象绑在一起的帮助。第一个选择器将用作参数,第二个选择器将用作带变量的开关,并在按钮被推到底部的显示器上时计算一些东西。我知道这是一个巨大的要求,但如果有人能指出我正确的方向,我会很高兴。

-(void)buttonTapped:(id)sender
{
    int maxRow = [myPicker selectedRowInComponent:0];
    int programRow = [myPicker selectedRowInComponent:1];

    NSLog(@"One Rep Max Chosen: %@", [oneRepMax objectAtIndex:maxRow];
    NSLog(@"Program Chosen: %@", [program objectAtIndex:programRow];

    NSString *allInformation = [NSString alloc] initWithFormat:@%@\n%@", [oneRepMax objectAtIndex:maxRow],[program objectAtIndex:programRow]];

    int level = programRow;
    int max = [[oneRepMax objectAtIndex:maxRow]intValue];
    int set1;
    int set2;
    int set3;
    int set4;

    switch(level)
    {
        case 0:
        {
             set1 = max * 0.7;
             set2 = max * 0.71;
             set3 = max * 0.72;
             set4 = max * 0.73;
             break;
        }
        case1:
    }

1 个答案:

答案 0 :(得分:0)

嗯,简短的回答是得到一本书,因为我不知道你对Objective C:D

了解多少

但这似乎是一个直截了当的问题,我可以提供一些关于它的方法的一般概念。

我将使用纯编码而不是Interface Builder,但我建议使用Interface Builder,因为它将接口设置与实际代码逻辑分开。

此代码假设使用了自动引用计数(ARC)

ViewController.h文件

#import <UIKit/UIKit.h>

// --------------------------------------------------------------------------------
// The UIPickerViewDelegate is a protocol (kinda of like an abstract class in C++)
// you need to implement the methods (or functions in C++ terms) of the protocol
//
// The UIPickerViewDataSource also a protocol to tell your ViewController to expect
// a method that provides your UIPickerViews the data it needs to display.
// --------------------------------------------------------------------------------
@interface ViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
{
    // UIPickerView can have multiple components (multiple roller thing)
    UIPickerView *myPicker;

    // arrays to hold your data
    NSArray *list1;
    NSArray *list2;

    // our button to do something when pressed
    UIButton *btnCalculate;
}

@end

ViewController.m文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    myPicker.showsSelectionIndicator = YES;

    // ------------------------------------------------------------------
    // these two lines are compulsory, it is saying the picker view's
    // delegate and datasource is this view controller itself
    // ------------------------------------------------------------------
    myPicker.delegate = self;
    myPicker.dataSource = self;

    // ------------------------------------------------------------------
    // Here we're specifying what we want to show in each row of the
    // picker view. Each of these array will be loaded into each of
    // the picker view components (see below)
    // ------------------------------------------------------------------

    // -------------------------------------
    // |    apple        |     option 1    |
    // |    banana       |     option 2    |
    // |    orange       |     option 3    |
    // |    mango        |                 |
    // |    peach        |                 |
    // -------------------------------------

    list1 = [[NSArray alloc] initWithObjects:@"apple", @"banana", @"orange", @"mango", @"peach", nil];
    list2 = [[NSArray alloc] initWithObjects:@"option1", @"option2", @"option3", nil];


    btnCalculate = [[UIButton alloc] initWithFrame:CGRectMake(120, 300, 100, 50)];
    btnCalculate.backgroundColor = [UIColor blackColor];
    [btnCalculate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnCalculate setTitle:@"Calculate" forState:UIControlStateNormal];

    // this is how we add a callback method for the button tap event
    [btnCalculate addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:myPicker];
    [self.view addSubview:btnCalculate];
}

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


// ------------------------------------------------------------
// Your UIPickerViewDataSource methods you need to implement
// ------------------------------------------------------------
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    // we're telling the our picker we want 2 rollers in a single picker view
    // like this:

    // -------------------------------------
    // |                 |                 |
    // |                 |                 |
    // |   component 1   |   component 2   |
    // |                 |                 |
    // |                 |                 |
    // -------------------------------------

    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // -------------------------------------------------------------------------
    // this is telling the picker how many rows to expect for each picker
    // it must match up with our data source (which in this case is our
    // list1 and list2).
    // -------------------------------------------------------------------------

    // -------------------------------------
    // |                 |                 |
    // |                 |                 |
    // |     5 fruits    |     3 options   |
    // |                 |                 |
    // |                 |                 |
    // -------------------------------------

    if(component == 0)
    {
        return list1.count;
    }
    else
    {
        return list2.count;
    }
}

// ------------------------------------------------------------
// Your UIPickerViewDelegate methods you need to implement
// ------------------------------------------------------------

// ------------------------------------------------------------
// This method will return the actual text into the picker view
// components.
//
// Note: This is for basic UIPickerView, if you want more fancy
// looking rows, you need to implement the other delegate method
// instead of this one.
// ------------------------------------------------------------
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
    {
        return [list1 objectAtIndex:row];
    }
    else
    {
        return [list2 objectAtIndex:row];
    }
}

// ------------------------------------------------------------
// Your callback method for when user taps on your button
// ------------------------------------------------------------
-(void)buttonTapped:(id)sender
{
    int fruitRow = [myPicker selectedRowInComponent:0];
    int optionRow = [myPicker selectedRowInComponent:1];

    // This will print the result into Xcode's console window
    NSLog(@"Fruit chosen: %@", [list1 objectAtIndex:fruitRow]);
    NSLog(@"Option chosen: %@", [list2 objectAtIndex:optionRow]);

    // This will show a popup alert view with same information
    NSString *allInformation = [[NSString alloc] initWithFormat:@"%@\n%@",
                                [list1 objectAtIndex:fruitRow], [list2 objectAtIndex:optionRow]];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Information"
                                                    message:allInformation
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil, nil];
    [alert show];
}


@end

最终结果

你应该得到这样的东西:

enter image description here

从那里你修改方法-(void)buttonTapped:(id)sender来做任何你喜欢的事。

希望有所帮助。

相关问题