作者istale (一天一次的嘆息,快用光ꜩ
看板MacDev
標題[問題] self的指向
時間Sun May 9 17:00:20 2010
Delegate.h
#import <UIKit/UIKit.h>
@class MyViewController;
@interface HelloWorld_iPhoneAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *myViewController;
}
@property (nonatomic,retain) IBOutlet UIWindow *window;
@property (nonatomic,retain) MyViewController *myViewController;
@end
-------------------
Delegate.m
#import "HelloWorld_iPhoneAppDelegate.h"
#import "MyViewController.h"
@implementation HelloWorld_iPhoneAppDelegate
@synthesize window;
@synthesize myViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
MyViewController *aViewController=[[MyViewController alloc]
initWithNibName:@"MyViewController"
bundle:[NSBundle mainBundle]];
[*[1;31mself setMyViewController:aViewController];
[aViewController release];
[window addSubview:[myViewController view]];
[window makeKeyAndVisible];
}
-------------------------
上面是xcode裡helloworld for iphone的sample code
我想問的是setMyViewController前的self,
從Delegate.h裡的宣告來看,這個self應該指myViewContorller
可是我用myViewController去替代self時,程式卻不能正確執行
那到底這個self指誰呢...謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.177.33
1F:→ leondemon:an instance of myViewController 05/09 17:10
2F:→ uranusjr:self 指的是那個 instance 本身, 就你的程式碼而言因為是 05/09 18:38
3F:→ uranusjr:HelloWorld_iPhoneAppDelegate 這個 class 裡的 self, 所 05/09 18:38
4F:→ uranusjr:以是那個屬於 HelloWorld_iPhoneAppDelegate 的 instance 05/09 18:38
5F:→ uranusjr:自己本身. 另外這詭異的 class naming 是打哪來的... 05/09 18:39
6F:→ leondemon:我沒仔細看類別名 的確是HelloWorld_iPhoneAppDelegate 05/09 19:02
7F:→ leondemon:的instance 05/09 19:03
8F:推 aecho:self就相當於C++中的this 05/14 23:01