1

私はこの問題の解決策を見つけようとしているさまざまなフォーラムを見てきました。現在、BluetoothViewControllerとAccountViewControllerの2つのクラスがあります。私がやろうとしているのは、ユーザーがAccountViewControllerから選択したアカウントをBluetoothViewControllerに渡すことです。これにより、後でBluetoothViewControllerでその文字列を使用できるようになります。私が行ったことは、BluetoothViewController.hにAccountViewControllerのインスタンスを作成し、AccountViewController.hファイルに文字列のプロパティを作成したことです。アクセスしようとしている文字列は「account8」です

私のAccountViewController.hファイル:

@interface AccountViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{

    NSArray     *tableData;
    int         cellValue;
    NSString    *cellContents;
    NSString    *account8;

}

@property (nonatomic, retain) NSArray   *tableData;
@property (nonatomic, retain) NSString  *cellContents;
@property (nonatomic, retain) NSString  *account8;

私のAccountViewController.mファイル:

#import "AccountViewController.h"


@interface AccountViewController ()

@end

// Implementing the methods of ViewController
@implementation AccountViewController

@synthesize tableData;
@synthesize cellContents;
@synthesize account8;


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    account8 = [tableData objectAtIndex:indexPath.row];

    BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
    [kAppDelegate setSelectedTabWithTag:-1];
    [self.navigationController pushViewController:bluetoothViewController animated:YES];

}

私のBluetoothViewController.h

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;

}


@property(nonatomic, retain) AccountViewController *classObj;

私のBluetoothViewController.mファイル:

#import "AccountViewController.h"
#import "BluetoothViewController.h"



@interface BluetoothViewController ()

@end

// Implementing the methods of ViewController
@implementation BluetoothViewController

- (void)viewDidLoad {

    [connect setHidden:NO];
    [disconnect setHidden:YES];
    [super viewDidLoad];

    count = 0;

    classobj = [[AccountViewController alloc] init];

    accountSelected = classobj.account8;

    accountSelection.text = accountSelected;
}

ユーザーがテーブルから行を選択すると、内容は変数account8に保存されます。その後、BluetoothViewControllerが呼び出されます。これで、BluetoothViewControllerがロードされると、ユーザーが選択したアカウントが表示されるはずです。ただし、ラベルは空白を返します。AccountViewControllerに保存されている変数に正しくアクセスできないのはなぜだろうか。

4

3 に答える 3

6

できることは、Bluetoothcontroller でインスタンス変数を作成し、そのオブジェクトをインスタンス化した後、AccountViewController から設定することです。例えば:

アカウント ビュー コントローラー:

BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];
bluetoothViewController.accountVariable = _account8;

BluetoothViewController:

@property (nonatomic, copy) NSString  *accountVariable;

または、他に必要な理由はありますclassobjか?

于 2013-01-23T15:39:00.857 に答える
0

以下のコードを確認してください。

AccountViewController.h ファイル:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>{

    AccountViewController   *classobj;
}

@property(nonatomic, retain) AccountViewController *classObj;

BluetoothViewController.h ファイル:

#import "AccountViewController.h"

@interface BluetoothViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate>

@property(nonatomic, retain) AccountViewController *classObj;

AccountViewController.m ファイル:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        account8 = [tableData objectAtIndex:indexPath.row];

        BluetoothViewController *bluetoothViewController = [[BluetoothViewController alloc] initWithNibName:@"BluetoothViewController" bundle:nil];

        [kAppDelegate setSelectedTabWithTag:-1];
[bluetoothViewController.view setAlpha:1.0f];

[bluetoothViewController setClassObj:self];

        [self.navigationController pushViewController:bluetoothViewController animated:YES];

    }

BluetoothViewController.m ファイル:

- (void)viewDidLoad {

    [connect setHidden:NO];
    [disconnect setHidden:YES];
    [super viewDidLoad];

    count = 0;

// Here classObj is already assigned to a reference so you can directly use that one
}

これがあなたを助けることを願っています。

于 2013-01-23T15:54:16.907 に答える
0

XCode を使用して、インポートを行い、オブジェクトをプロパティとして宣言して作成し、「object.variable」構文を使用する必要があります。ファイル「AccountViewController.m」は次のようになります。

#import AccountViewController.h
#import BluetoothViewController.h;

@interface AccountViewController ()
...
@property (nonatomic, strong) BluetoothViewController *bluetoothViewController;
...
@end

@implementation AccountViewController

//accessing the variable from balloon.h
...bluetoothViewController.variableFromBluetoothViewController...;

...
@end
于 2014-11-12T03:28:23.567 に答える