1

はじめに、私はiOS開発に不慣れです。私はしばらくの間、ここでもGoogleでも、答えを見つけるために周りを見回してきました。

私のアプリケーションは、注釈付きのマップビューにロードされます。ユーザーが注釈の1つをタップすると、アクセサリボタンのあるコールアウトビューが表示されます。私が抱えている問題は、アクセサリボタンをタップしたときに呼び出されるメソッドにあります。特定の注釈の詳細ビューを表示したいのですが、アクセサリボタンをタップすると、アプリがSIGABRTでクラッシュします。

// method that provides the view for when the callout accessory button is tapped
-               (void)mapView:(MKMapView *)mapView 
               annotationView:(MKAnnotationView *)view 
calloutAccessoryControlTapped:(UIControl *)control
{
    // grabs the annotation's title from the annotation view
    NSString *viewTitle = view.annotation.title;

    // grabs corresponding POI object for map annotation
    PointOfInterest *object = [[PointOfInterest alloc] init];
    object = [dictionary objectForKey:(NSString *)viewTitle];

    // assigns title and subtitle ivars to variables to be passed into detailviewcontroller init
    NSString *title = object._title;
    NSString *subtitle = object._subtitle;
    UIImage *picture = object._picture;
    NSString *description = object._description;

    // releases POI object after all the information has been taken from it
    [object release];

    // inits detailVC
    DetailVC *detailVC = [[DetailVC alloc] initWithNibName:@"DetailVC" 
                                                    bundle:[NSBundle mainBundle]];

    // sets the nsstring ivars in the DVC which correspond to the POI information
    detailVC.thetitleText = title;
    detailVC.thesubtitleText = subtitle;
    detailVC.thepictureImage = picture;
    detailVC.thedescriptionText = description;

    // sets the "back" button on the navigation controller to say "Back to Map"
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back to Map" 
                                                                      style:UIBarButtonItemStyleBordered 
                                                                     target:nil 
                                                                     action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];
    [newBackButton release];

    // pushes navcontroller onto the stack and releases the detail viewcontroller
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

タイトルとサブタイトルでビューを表示しようとしているだけなので、画像と説明はまだ追加していません。

DetailViewControllerクラスコード ヘッダーは次のとおりです。

@interface DetailViewController : UIViewController {
    IBOutlet UIScrollView *scrollview;
    IBOutlet UILabel *thetitle;
    NSString *thetitleText;
    IBOutlet UILabel *thesubtitle;
    IBOutlet UIImage *thepicture;
    IBOutlet UITextView *thedescription;
}

@property (nonatomic, retain) UIScrollView *scrollview;
@property (nonatomic, retain) UILabel *thetitle;
@property (nonatomic, retain) NSString *thetitleText;
@property (nonatomic, retain) UILabel *thesubtitle;
@property (nonatomic, retain) UIImage *thepicture;
@property (nonatomic, retain) UITextView *thedescription;

// method that creates the custom detail view with the corresponding information from
// the point of interest objects
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString*)title subtitle:(NSString *)subtitle picture:(UIImage *)picture description:(NSString *)description;

@end

実装:

@implementation DetailViewController

@synthesize scrollview, thetitle, thesubtitle, thepicture, thedescription, thetitleText;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)title subtitle:(NSString *)subtitle picture:(UIImage *)picture description:(NSString *)description;
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        NSLog(@"view initialized");
}
return self;
}

- (void)dealloc
{
    [scrollview release];
    [thetitle release];
    [thesubtitle release];
    [thepicture release];
    [thedescription release];
    [thetitleText release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");

    [thetitle setText:self.thetitleText];
    NSLog(@"thetitle text set");


// Do any additional setup after loading the view from its nib.
//    [thetitle setText:title];
//    NSLog(@"set title");
//    [thesubtitle setText:subtitle];
//    NSLog(@"set subtitle");
//    thepicture = picture;
//    [thedescription setText:description];
//    NSLog(@"set description");
}

SIGABRTの出力は次のとおりです。

2011-07-12 19:05:06.678 mkeBOAT [1687:ef03]コールアウトアクセサリがタップされました

2011-07-12 19:05:06.679 mkeBOAT [1687:ef03]USバンクタワー

2011-07-12 19:05:06.680 mkeBOAT [1687:ef03](null)

2011-07-12 19:05:06.680 mkeBOAT [1687:ef03](null)、(null)

2011-07-12 19:05:06.680 mkeBOAT [1687:ef03]ビューが初期化されました

2011-07-12 19:05:06.711 mkeBOAT [1687:ef03] *キャッチされなかった例外のためにアプリを終了します

'NSUnknownKeyException'、理由:'[setValue:forUndefinedKey:]:このクラスは、キーの説明に対してキー値コーディングに準拠していません。

実際に何かがあるはずの値に対してnullを出力しているため、辞書が正しく機能しているとは思いません。

助けてくれてありがとう!

4

7 に答える 7

3

あなたはこれをすべきではありません

[detailViewController autorelease];

detailViewControllerをスタックにプッシュする前。

最初にナビゲーションスタックにプッシュしてから解放する必要があります。これは、もう必要がなく、ナビゲーションスタックがdetailViewコントローラーオブジェクトの所有者になっているためです。

だからこれをする代わりに

[detailViewController autorelease];

// this is the line that breaks with SIGABRT
[self.navigationController pushViewController:detailViewController animated:YES];

これを行う

[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

より詳しい情報:

SIGABRTは通常、IBに参照アウトレットが残っているが、urコントローラーのオブジェクトがもう存在しない場合に発生します。

すべての参照先を確認する必要があります。

于 2011-07-12T22:44:13.497 に答える
0

私はあなたがそれにあなたの@propertyセットを持っているからだと思いcopyますretain

于 2011-07-12T22:35:03.223 に答える
0

XIBのファイル所有者のクラスがDetailViewControllerに設定されていることを確認してください。

于 2011-07-12T22:39:14.600 に答える
0

実装にいくつかのエラーがあります:

1)固定値で親メソッドを呼び出さないでください:

self = [super initWithNibName:@ "DetailViewController" bundle:nil];

代わりにパラメータを使用してください:

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

2)タイトルとサブタイトルのプロパティはIBOutlet UILabelですが、保持する代わりにコピーを使用し、保持してコピーを変更します

3)initメソッドでUILabel(タイトルとサブタイトル)テキストを初期化しますが、このメソッドでは、プロパティはまだ定義されていません(タイトルとサブタイトル= nil)。

代わりに、NSStringプロパティを使用し、NSStringプロパティを使用してviewDidLoadメソッドでUILabelのテキストを設定する必要があります。

また、UITextFieldのdescriptionプロパティについても同じようにします。

また、deallocにコピーまたは保持されているすべてのプロパティを解放し、viewDidUnloadメソッドでIBOutletプロパティをリセットすることを忘れないでください。

ポイント3について:

NSStringプロパティをDetailViewControllerに追加します(そして、deallocで合成してリリースすることを忘れないでください):

@property(nonatomic、retain)NSString * thetitleText;

次に、pushViewControllerの前に、次を追加します。

detailViewController.thetitleText=タイトル;

最後に、DetailViewControllerのviewDidLoadメソッドに、次を追加します。

[タイトルsetText:self.thetitleText];

他のラベルについても同じようにします。

于 2011-07-12T23:01:45.310 に答える
0

再起動して新しいDetailViewControllerを作成した後、実際のビューではなく、ビューが画像にリンクされていることを確信しています。これをさらに複雑にするために、私はまた、私のスクロールビューが本当に物事を台無しにしていたと信じています。誰かが実際に機能するコードを見たい場合は、私に知らせてください。貢献してくれたすべての人に感謝します!! なんて素晴らしいコミュニティSOverflowです!

于 2011-07-13T07:11:28.233 に答える
0

コードを注意深く読んでください...

を使用して初期化しましたobject

PointOfInterest *object = [[PointOfInterest alloc] init];

objectなぜそのように再割り当てするのですか:

object = [dictionary objectForKey:viewTitle];

したがって、この問題に関係なく、ここにリークがあります。また、クラッシュはobject、対応するキーを使用して値に再割り当てし、viewTitle後でメッセージを送信しようとしたこと、およびタイプのオブジェクトを操作することを考えていることが原因であると確信していますPointOfInterest

お役に立てれば。

于 2011-07-13T20:25:26.167 に答える
0

ViewControllerにビューがあることを確認してください。ビューアウトレット(通常はxibファイルで作成されます)をコントローラーのビューに接続することをIBで忘れていたため、質問で説明したように、私の爆撃(SIGABRT)が発生しました。

于 2012-02-03T16:52:35.040 に答える