2011-02-15 131 views
2

我想更多地瞭解了對象 - 宏編程,因爲我已經看到了相當多的用它做很酷的東西。是否可以用一行宏來完成以下內容?對象 - 宏編程

MyNewViewController *newVC = [[MyNewViewController alloc] init]; 
[self.navigationController pushViewController:newVC animated:YES]; 
[newVC release]; 

喜歡的東西:

PushToNavController(@"MyNewViewController",YES); 

感謝

+0

考慮使用功能這一點。宏對於宏來說太大了。 – 2011-02-15 20:10:31

+6

和幅度更難以在功能有限的/不增加調試的順序。 – kubi 2011-02-15 20:17:30

回答

5

肯定的:

#define PushToNavController(_n,_a) { \ 
_n *__vc = [[(_n) alloc] init]; \ 
[self.navigationController pushViewController:__vc animated:(_a)]; \ 
[__vc release]; \ 
} 

然後你會使用這樣的:

PushToNavController(MyNewViewController, YES); 

但是..你爲什麼要這樣做?