使用多个变量准备ForSegue

时间:2015-10-25 00:26:36

标签: ios objective-c iphone cocoa-touch variables

背景:这是我的第二部iphone应用程序,但我已经完成了其他类型的编码。

我正在通过本教程创建iOS音乐播放器应用: Build an iOS Music Player

情况非常好:到目前为止,我可以填充一张包含iPhone播放列表的表格。我可以选择一个播放列表,获取该播放列表的持久ID,并使用它在下一页上创建查询,该页面用该播放列表中的所有歌曲填充表格,并显示歌曲的数量和总分钟数。

只要您只传递一个变量(在本例中为专辑名称),本教程中的代码就可以运行 - 这在教程中给出了:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AlbumViewController *detailViewController = [segue destinationViewController];

    MPMediaQuery *albumsQuery = [MPMediaQuery albumsQuery];
    NSArray *albums = [albumsQuery collections];

    int selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    MPMediaItem *selectedItem = [[albums objectAtIndex:selectedIndex] representativeItem];
    NSString *albumTitle = [selectedItem     valueForProperty:MPMediaItemPropertyAlbumTitle];
//THIS IS WHAT'S PASSING THE VARIABLE                     
    [detailViewController setAlbumTitle:albumTitle];
}

我已将其更改为此 - 以便能够使用播放列表,因为我发现使用持久性ID比制定结果查询的名称更有效(此代码有效):

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    PlaylistSongsViewController *varLetsSegueOhYeah = [segue destinationViewController];
    //what are we even looking at again? it's a query
    MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
    //identify the query again please?
    NSArray *playlists = [playlistsQuery collections];
    //where are we in the table
    int selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    //for that index id, what playlist does that map to
    MPMediaItem *selectedItem = [playlists objectAtIndex:selectedIndex];
    //for that playlist, what is its name
    //NSString *varPlaylistName = [selectedItem     valueForProperty:MPMediaPlaylistPropertyName];
    //for that playlist, what is its persistent id
    NSString *varPlaylistID = [selectedItem     valueForProperty:MPMediaPlaylistPropertyPersistentID];
//THIS IS WHAT'S PASSING THE VARIABLE
    [varLetsSegueOhYeah setVarPlaylistID:varPlaylistID];
}

然而,我希望能够传递两个变量 - 持久性ID和播放列表名称,因此我可以在目标屏幕的顶部显示它。您可以在上面的代码中看到实际抓取播放列表名称的行被注释掉了 - 这是因为显然如果您抓住了值但实际上并没有使用它,那么应用程序就会崩溃。但它可以向您展示如果我知道如何使用它将会是什么样子。这两个变量都在目标屏幕的ViewController.h中正确声明。

我已经搜索了各种猜测和组合,但到目前为止还没有任何工作。总而言之 - 我知道如何发送1个变量,现在我该如何发送2个?

编辑:目标屏幕代码

PlaylistSongsViewController.m 从这样开始:

#import "PlaylistSongsViewController.h"

@interface PlaylistSongsViewController ()

@end

@implementation PlaylistSongsViewController
@synthesize varPlaylistName;
@synthesize varPlaylistID;
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = varPlaylistName;
}

其余部分可以在"第3步:显示专辑歌曲&中找到here。信息"除了我已经取代"专辑"用"播放列表"。

PlaylistSongsViewController.h:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@interface PlaylistSongsViewController : UITableViewController
{
    NSString *varPlaylistName;
    NSString *varPlaylistID;
}
//albumTitle in tutorial

@property NSString *varPlaylistName;
@property NSString *varPlaylistID;

@end

3 个答案:

答案 0 :(得分:0)

您可以根据需要设置任意数量的变量:

[varLetsSegueOhYeah setVarPlaylistID:varPlaylistID];
[varLetsSegueOhYeah setVarPlaylistName:varPlaylistName];

您也可以直接访问该物业:

varLetsSegueOhYeah.varPlaylistID = varPlaylicstID;
varLetsSegueOhYeah.varPlaylistName = varPlaylistName;

然后在viewDidLoad

NSLog(@"varPlaylistID = %@",self.varPlaylistID);
NSLog(@"varPlaylistName = %@",self.varPlaylistName);

确保在接口文件中声明属性:

@property (nonatomic , strong) NSString* varPlaylicstID;
@property (nonatomic , strong) NSString* varPlaylistName;

请注意,您不需要像过去那样合成您的属性,它现在就会自动完成。

答案 1 :(得分:0)

试试这个:

if ([segue.destinationViewController respondsToSelector:@selector(setVarPlaylistID:)]) {

        [segue.destinationViewController setVarPlaylistID:varPlaylistID];
        [segue.destinationViewController setVarPlaylistName:varPlaylistName];
        [segue.destinationViewController setAnotherVariable:variableNameOfCorrectType];

} else NSLog(%@"Segue does not respond to selector");

在destinationViewController中,您必须如此声明和合成:

NSString* varPlaylistID;
.
.
.
@synthesize varPlaylistID = _varPlaylistID;

//Declare setter
-(void) setVarPlaylistID:(NSString *)varPlaylistID
{
_varPlaylistID = varPlaylistID;
}

对varPlaylistName执行相同的操作。

答案 2 :(得分:0)

好的 - 我终于开始工作了。我发现了我的问题:

  1. 我没有意识到我们并没有那么多&#34;传递全局变量&#34;因为我曾经把它们当作独立的信息包围。他们是 更多类似的表单字段附加到每个对象类型和我们 发送填写说明。
  2. 因为我认为我的混乱思维过程让编译器感到困惑。
  3. 摆脱&#34; @ synthesize&#34;后,我无法弄清楚它何时需要&#34; _&#34;在财产名称之前以及何时需要&#34; self。&#34;。我排序 通过反复试验和更清楚地完成了这一点 关于概念上发生了什么的想法。

    1. 我没有意识到我在编译器中设置了断点,导致它在UI中挂起,否则就不会。糟糕。
  4. New PlaylistsController.h文件:

    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    @interface PlaylistsViewController : UITableViewController
    
    
    @property (nonatomic, retain) NSString *varSelectedPlaylistID;
    @property (nonatomic, retain) NSString *varSelectedPlaylistName;
    
    
    @end
    

    这说明我们将要找出用户为播放列表ID和播放列表名称选择的值。

    New PlaylistSongsController.h:

    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>
    
    @interface PlaylistSongsViewController : UITableViewController
    
    @property (nonatomic, retain) NSString *varPlaylistIDToDisplay;
    @property (nonatomic, retain) NSString *varPlaylistNameToDisplay;
    
    @end
    

    这说明我们将使用某些值来显示特定的播放列表名称和ID。

    新播放列表控制器PrepareForSegue:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        //First - identify what the user has selected.
    
        //define query
        MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
        //dump query results into array
        NSArray *playlists = [playlistsQuery collections];
    
        //identify user selected table row
        int selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        //map user selected table row to array item
        MPMediaItem *selectedItem = [playlists objectAtIndex:selectedIndex];
    
    
        //assign selected values to properties of the object we're looking at right now. They have underscores here for some reason!
        _varSelectedPlaylistID = [selectedItem valueForProperty:MPMediaPlaylistPropertyPersistentID];
        _varSelectedPlaylistName = [selectedItem valueForProperty:MPMediaPlaylistPropertyName];
    
    
        //now let's get ready to go. for an object of type "Playlist Songs View Controller, we want to create a new instance of that and name it MyNewPlaylistInstance and then go to there.
        PlaylistSongsViewController *MyNewPlaylistInstance = [segue destinationViewController];
    
        //when we get to MyNewPlaylistInstance, it will want to know which playlist ID and name to use - tell it to use the values we just set
        MyNewPlaylistInstance.varPlaylistIDToDisplay = self.varSelectedPlaylistID;
        MyNewPlaylistInstance.varPlaylistNameToDisplay = self.varSelectedPlaylistName;
    }
    
        @end