【问题标题】:Create relationships between different ViewControllers在不同的 ViewController 之间创建关系
【发布时间】:2014-01-16 19:51:51
【问题描述】:

我的实体之间有以下关系。

我有 2 个视图控制器。 1 添加播放列表的位置和 1 添加歌曲的位置。那么我的问题是,歌曲 ViewController 如何知道它需要将歌曲与哪个播放列表关联并创建与该确切播放列表的关系?

PlaylistViewController.m(这是我保存播放列表的地方)

-(void)saveTap:(id)sender{
    Playlists *newManagedObject = (Playlists*)[NSEntityDescription insertNewObjectForEntityForName:@"Playlists" inManagedObjectContext:_managedObjectContext];
    [newManagedObject setValue:self.textfield.text forKey:@"playlistName"];


    // Save the context.
    NSError *error = nil;
    if (![_managedObjectContext save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();

    }

    [self fetchDevices];

}

SongsViewController.m(这是保存歌曲的地方)

-(IBAction)songsDone:(id)sender{

    AppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];
    NSManagedObject *newManagedObject;
    newManagedObject = [NSEntityDescription
              insertNewObjectForEntityForName:@"Songs"
              inManagedObjectContext:context];
    NSDate *today= [NSDate date];


    [newManagedObject setValue:video.author forKey:@"author"];
    [newManagedObject setValue:video.videoid forKey:@"link"];
    [newManagedObject setValue:video.title forKey:@"songName"];
    [newManagedObject setValue:today forKey:@"created"];



    // Save the context.
    NSError *error = nil;
    if (![_managedObjectContext save:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();

}

Song.h(实体)

@class Playlists;

@interface Songs : NSManagedObject

@property (nonatomic, retain) NSString * author;
@property (nonatomic, retain) NSDate * created;
@property (nonatomic, retain) NSString * link;
@property (nonatomic, retain) NSString * songName;
@property (nonatomic, retain) NSSet *songs;
@end

@interface Songs (CoreDataGeneratedAccessors)

- (void)addSongsObject:(Playlists *)value;
- (void)removeSongsObject:(Playlists *)value;
- (void)addSongs:(NSSet *)values;
- (void)removeSongs:(NSSet *)values;

@end

Playlists.m(实体)

@class Songs;

@interface Playlists : NSManagedObject

@property (nonatomic, retain) NSString * playlistName;
@property (nonatomic, retain) NSSet *list;
@end

@interface Playlists (CoreDataGeneratedAccessors)

- (void)addListObject:(Songs *)value;
- (void)removeListObject:(Songs *)value;
- (void)addList:(NSSet *)values;
- (void)removeList:(NSSet *)values;

@end

【问题讨论】:

  • 我不是在问如何在视图控制器之间传递数据。我在问我如何通过使用关系将歌曲与播放列表相关联,而无需像 SQL 中那样具有自动增量 ID。

标签: ios objective-c


【解决方案1】:

您需要为用户提供某种方式来选择应该与歌曲关联的播放列表。 2 个常规选项:

  1. 选择一个播放列表以查看和编辑其中包含的歌曲(因此播放列表从第一个控制器传递到第二个控制器)。
  2. 显示歌曲详细信息并有一个按钮来显示播放列表选择器(可以是选择器视图或表格视图)。这使用委托将播放列表返回给歌曲控制器。

拥有playlistsong 托管对象后,请使用:

[playlist addSongsObject:song];

在保存上下文之前(这将更新两个对象的关系,因为它们是反向的)。

【讨论】:

  • 这些东西我已经有了。问题是我如何将歌曲与播放列表相关联。我已经尝试通过播放列表 ObjectID 或 indexpath.row 关联歌曲,但它们会在删除一行并重新启动应用程序时发生变化。
  • 以及这里的歌曲如何知道选择了另一个 ViewController 中的哪一个。我需要在视图控制器之间传递一些东西吗?
  • 是的,您需要将播放列表对象传递给歌曲视图控制器。您需要拥有两个托管对象实例才能配置关系。
  • 我通过 Singleton optionsSingle = [rowNumber singleObj] 传递数据; optionsSingle = [设备 objectAtIndex:indexPath.row];这就是传递托管对象的意思吗? (devices 是所有播放列表所在的 NsMutableArray)
  • 不清楚那是什么。您的控制器应该能够访问PlaylistsSongs 的实例,以便连接它们。我猜将使用 2 个不同的控制器来选择它们。您需要将选择的第一个实例传递给第二个控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-05
  • 1970-01-01
  • 2013-01-14
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多