【问题标题】:Give out a NSMutableArray on a texView on another Viewcontroller in objective-C?在objective-C中的另一个Viewcontroller上的texView上给出一个NSMutableArray?
【发布时间】:2012-01-02 19:48:19
【问题描述】:

当我按下这个 viewContoller 上的按钮时,我尝试将一个 viewController 上的文本字段中的数字保存在 NSMutableArray 中。 (现在正在工作) 然后我希望在 secondViewController 上的 textview 上给出数字,但这不起作用。当我想在第一个 Viewcontroller 上给出数组时,它工作正常。 我也不能用 SecondviewController 上的按钮擦除 NSMutableArray。

SecondviewController 与 viewController 具有相同的类。

谁能告诉我如何在 seconviewcontroller 上给出一个数组?

你好, 目前我有这个:

//ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
NSMutableArray *textViewArray;
}
@property (strong, nonatomic) IBOutlet UITextField *textLable2;
@property (strong, nonatomic) IBOutlet UITextField *textLable1;

- (IBAction)setArrayWithCurrentNumber:(id)sender;
- (IBAction)returnToTextfield:(id)sender;

@end

//这个.m文件:

#import "ViewController.h"

@implementation ViewController
@synthesize textLable2;
@synthesize textLable1;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    textViewArray = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setTextLable2:nil];
    [self setTextLable1:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)setArrayWithCurrentNumber:(id)sender 
{
    NSString *string1 = self.textLable1.text;
    [textViewArray addObject:string1];
    NSMutableArray *array = [NSMutableArray arrayWithArray:textViewArray];
    NSString *string2 = [array componentsJoinedByString:@" "];

    self.textLable2.text = [NSString stringWithString:string2];
    NSLog(@"%@",textViewArray);
}

- (IBAction)returnToTextfield:(id)sender 
{
    [textLable1 resignFirstResponder];
    [textLable2 resignFirstResponder];
}
@end

【问题讨论】:

    标签: iphone objective-c cocoa


    【解决方案1】:

    如果您从第一个视图控制器调用第二个视图控制器,您可以在第二个视图控制器上设置一个属性来保存 NSMutableArray 或将其发送到初始化程序。

    类似:

    - (id)initWithArray:(NSMutableArray *)array {
        if (self = [super init]) {
            myArray = [array copy];
        }
        return self;
    }
    

    假设您的第二个视图控制器已声明 NSMutableArray * myArray;

    编辑:在此处添加更多代码...

    // I'll assume you use some kind of UINavigationController to show your content
    - (void)showSecondViewController {
        SecondViewController * vc = [[[SecondViewController alloc] initWithArray:yourMutableArray] autorelease];
        [self.navigationController pushViewController:vc];
    }
    

    【讨论】:

    • 我如何在初始化器上发送它?
    • 我已经添加了第二个vc初始化。
    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多