关于iphone:将对象发送到UITableView控制器

Send object to UITableView controller

希望有人能帮我,这让我很为难

我的应用程序在选项卡栏中有两个视图控制器…

1
2
3
4
5
6
7
8
9
10
11
12
13
//Create two view controllers
UIViewController *vc1 = [[RecordingViewController alloc] init];
UIViewController *vc2 = [[SavedViewController alloc] init];//this is a tableViewController

//Make an array with the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

//Release the two view controllers since they are now retained by the array
[vc1 release];
[vc2 release];

//Attach the ViewControllers to the tabBar
[tabBarController setViewControllers:viewControllers];

然后我有另一个对象(fileHandler,它想将一个名为capture的对象发送到vc2以添加到NSMutableArray中,所以我从该类发送…

1
[vc2 addToCapturesArray:capture];

当然,这不起作用,我只是从编译器得到"VC2未声明"的消息。我如何让我的fileHandler类了解VC2实例?事先谢谢你的帮助。

谢谢你的帮助。它现在全部编译,但是VC2中的方法没有被调用出于某种原因。代码现在看起来像这样…

1
2
3
4
5
6
7
8
    @interface Rolling_VideoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;

//Tab bar
UITabBarController *tabBarController;

//Create an ivar so that the app delegate can hold a reference
SavedViewController *_vc2;

}@属性(非原子,保留)IBoutlet UIWindow*窗口;//创建一个属性以便我们可以从外部访问VC2@属性(非原子,保留)savedView控制器*vc2;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import"Rolling_VideoAppDelegate.h"

//Import view controllers
#import"RecordingViewController.h"
#import"SavedViewController.h"

@implementation Rolling_VideoAppDelegate

@synthesize window;
@synthesize vc2 = _vc2;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


//Create an instance of tabBarView controller
tabBarController = [[UITabBarController alloc]init];

//Create two view controllers
UIViewController *vc1 = [[RecordingViewController alloc] init];
UIViewController *vc2 = [[SavedViewController alloc] init];
self.vc2 = _vc2;


//Make an array with the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];

//Release the two view controllers since they are now retained by the array
[vc1 release];
[vc2 release];

//Attach the ViewControllers to the tabBar
[tabBarController setViewControllers:viewControllers];

//Place  the tabBar on the window
[window addSubview:[tabBarController view]];

[self.window makeKeyAndVisible];

return YES;

}

文件处理程序如下…

1
2
3
4
5
6
7
8
9
10
    Captures *capture = [[Captures alloc]initWithVideoPath:savePath];
NSLog(@"Instance of capture called:
 VideoPath: %@
 Geo: %@,
 UserNotes: %@", [capture videoPath], [capture location], [capture userNotes]);

// Get a reference to the app delegate, and then access the vc2 property
Rolling_VideoAppDelegate *appDelegate = (Rolling_VideoAppDelegate*)[UIApplication sharedApplication].delegate;

[appDelegate.vc2 addToCapturesArray:capture];

再次感谢你的帮助。

丰富的


假设您的VC1/VC2在AppDelegate中,那么您希望扩展类似这样的应用程序委托

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@interface AppDelegate
{
    // Create an ivar so that the app delegate can hold a reference
    VC2Class *_vc2;
}

// Create a property so that you can access if from the outside
@property(nonatomic, retain) VC2Class *vc2;

@end

@implementation AppDelegate

// Map the property to the ivar
@synthesize vc2 = _vc2;

// When you are freeing the app delegate make sure to nil
// the property to release it
-(void) dealloc
{
    self.vc2 = nil;
    [super dealloc];
}

在创建VC2时,除了其他代码外,还要执行以下操作:

1
2
3
// Since this is a retain property it will do the retain you need
// when assigning to self.vc2
self.vc2 = vc2;

现在,在你的文件namanger中,执行以下操作:

1
2
3
// Get a reference to the app delegate, and then access the vc2 property
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
VC2Class *vc2 = appDelegate.vc2;

然后你可以正常的打电话来。确保在文件名顶部包含#import"VC2Class.h"

@结束