我已經創建了一個應用程序,該應用程序在輸入用戶信息後向登錄頁面開始,並向服務器發送GET。使用響應值我動態地創建按鈕,標題等。一切正常,但在發送GET並使用JSON響應之後,我的按鈕正在與登錄頁面在同一視圖上創建。我也想創建一個新視圖並在該視圖上創建按鈕。由於我使用用戶信息中的一些參數,我需要使用MainViewController在同一個類中創建視圖(在我的情況下,它的格式爲BNT_1ViewController
),但我無法弄清楚如何處理這種情況。我也在分享代碼,也許有幫助。
第二個問題,使用此代碼我的按鈕只是向下移動,但我需要兩行按鈕。我怎樣才能改變x軸按鈕的位置?我改變這些值,但是整列變化,即使我想只是放置兩個按鈕在同一行,所有的結構取代了..如何在新視圖中顯示方法的結果?
-(IBAction)Accept:(id)sender
{ userName=[[NSString alloc] initWithString:userNameField.text ];
[userNameField setText:userName];
NSUserDefaults *userNameDef= [NSUserDefaults standardUserDefaults];
[userNameDef setObject:userName forKey:@"userNameKey"];
password =[[NSString alloc] initWithString:passwordField.text];
[passwordField setText:password];
NSUserDefaults *passDef=[NSUserDefaults standardUserDefaults];
[passDef setObject:password forKey:@"passwordKey"];
serverIP=[[NSString alloc] initWithString: serverField.text];
[serverField setText:serverIP];
NSUserDefaults *serverDef=[NSUserDefaults standardUserDefaults];
[serverDef setObject:serverIP forKey:@"serverIPKey"];
[userNameDef synchronize];
[serverDef synchronize];
[passDef synchronize];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
message:@"Your User Informations are going to be sent to server. Do you accept?"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel", nil];
[message show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
if([userNameField.text isEqualToString:@"" ]|| [passwordField.text isEqualToString:@""] || [serverField.text length]<10)
{
UIAlertView *message1 = [[UIAlertView alloc] initWithTitle:@"BNTPRO "
message:@"Your User Informations are not defined properly!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[message1 show];
[userNameField resignFirstResponder];
[passwordField resignFirstResponder];
return;
}
//## GET code to here**
NSString *str1=[@"?username=" stringByAppendingString:userNameField.text];
NSString *str2=[@"&password=" stringByAppendingString:passwordField.text];
NSString *str3=[str1 stringByAppendingString:str2];
NSString *str4 =[@"http://" stringByAppendingString:serverField.text];
NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]];
NSLog(@"%@\n",url);
//get the url to jsondata
NSData *jSonData=[NSData dataWithContentsOfURL:url];
if (jSonData!=nil) {
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:jSonData options:
NSJSONReadingMutableContainers error:&error];
if (error==nil) {
NSDictionary *mess=[result objectForKey:@"message"];
NSDictionary *messContent=[mess valueForKeyPath:@"message"];
NSDictionary *messDate=[mess valueForKeyPath:@"date"];
NSDictionary *messID=[mess valueForKeyPath:@"ID"];
NSLog(@"%@ *** Message %@ \n Message Content: %@ \n Mesage ID: %@ \n Message Date: %@", result, mess, messContent, messID,messDate);
NSString*key1=[ result objectForKey:@"key" ];
NSString *s1=[@"http://" stringByAppendingString:serverField.text];
NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];
NSLog(@"\n%@\n",url2);
NSData *data2=[NSData dataWithContentsOfURL:url2];
id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
//i create buttons from here
self.buttons = [NSMutableArray array];
CGFloat yPosition = 43.0f;//measure from top point
CGFloat xPosition = 34.0f;
const CGFloat buttonHeight = 70.0f;//height
const CGFloat buttonMargin = 32.0f;//distance between two buttons
for(NSDictionary* buttonData in result2) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle = [buttonData objectForKey:@"name"];
[button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor cyanColor]];
[button setTitle:buttonTitle forState:UIControlStateNormal];
// button.frame = CGRectMake(20.0f, yPosition, 130.0f, buttonHeight);//if([count]%2==1)
NSLog(@"Ne oluyor?= %d",[buttonData count]);
button.frame = CGRectMake(170.0f, yPosition, 130.0f, buttonHeight);//if([count]%2==1)
[button addTarget:self action:@selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.buttons addObject:button];
yPosition+= buttonHeight + buttonMargin;
} }
}
else if([title isEqualToString:@"Cancel"])
{
NSLog(@"You changed your mind!");
}
}
首先感謝這個指導方針,但正如我在問題的第一句中指出的那樣,我在目標C中不是很瞭解,你能否很快說出我必須使用哪些類?或者任何鏈接到相關的問題,教程將是偉大的..再次感謝 – 2012-01-05 19:03:56
你可以谷歌這一點,很多教程都存在there.Like之一是 - http://www.edumobile.org/iphone/iphone-編程 - 教程/ navigationcontroller應用功能於iphone / – rishi 2012-01-06 04:03:33