2015-07-21 71 views
-3

我已經創建了一個內容視圖來獲取來自JSon的消息,但我想隨機加載。加載隨機VOID方法

我有2個方法:

- (void)radioNews {} 
- (void)topNews {} 

現在我想獲得隨機radioNews或的TopNews在我viewDidLoad

- (void)viewDidLoad { 
      [self radioNews]; 
    } 

這是正確的只加載一個方法,可以從radioNews告訴getRandom和topNews然後在didLoad [self methodResult]

感謝

+0

我會懷疑'radioNews'和'topNews'是非常相似,僅在某些搜索參數方面有所不同。因此,確定在數據上有什麼不同和隨機性,而不是調用的方法。你肯定是重複了很多代碼。 – trojanfoe

+0

是json和不同數組的兩個不同服務器,所以更簡單快捷的做兩個方法。你給我-1的假設?程序員的世界被像你這樣的人毀了,無論如何感謝提示 – BlackSheep

回答

2

嘗試

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSInteger random = arc4random()%2; 
    if (random == 0) { 
     [self radioNews]; 
    }else{ 
     [self topNews]; 
    } 
} 
+0

感謝它的工作! – BlackSheep

0

可以從NSString 開始叫selector創建NSArrayNSString

// Define an NSArray of NSString, where a single string is the method name 
NSArray *methods = @[@"radioNews", @"topNews"]; 
// Retrieve a random index 
NSUInteger index = arc4random()%methods.count; 
// You can use objectAtIndex without check the array bounds because 
// the mod operation you did before will always return a value in bounds 

// Create a selector instance from a string 
SEL randomSelector = NSSelectorFromString([methods objectAtIndex: index]); 
// Call the selector 
[self performSelector:randomSelector 
      withObject:nil];