Take a random Number from one ViewController and use it in a second ViewController - Update - iphone

I have two ViewControllers: The RedButtonViewController and the TweetViewController. The RedButtonViewController generates random numbers in Textlabels and I want to use the number or the label with the TweetViewController. How can I make this?
Thanks for your help!
My TweetViewController will be opened with this code in the RedButtonViewController:
- (IBAction)TweetViewController:(id)sender {
TweetViewController *Tweet = [[TweetViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Tweet animated:YES];
}
Here is an exemple of how I generate the random number:
- (IBAction)pushRedButton:(UIButton *)sender {
int ZahlDerToten;
ZahlDerToten = arc4random() % 1000000;
outputLabel.text = [NSString stringWithFormat:#"You killed %d people.", ZahlDerToten];

Create a property on the TweetViewController and set it before presenting it:
- (IBAction)TweetViewController:(id)sender {
// Note: don't put leading capitals on a local variable
TweetViewController *tweet = [[TweetViewController alloc] initWithNibName:nil bundle:nil];
tweet.randomNumber = [self generateRandomNumber];
[self presentModalViewController:tweet animated:YES];
// Note: You were leaking the view controller
[tweet release];
}
Another solution (and how I usually do this kind of thing) is to create a new initializer called -initWithNumber: (probably something a little more descriptive than "number") and call it like this:
- (IBAction)TweetViewController:(id)sender {
TweetViewController *tweet = [[TweetViewController alloc] initWithNumber:[self generateRandomNumber]];
[self presentModalViewController:tweet animated:YES];
[tweet release];
}
-initWithNumber would then look something like:
- (TweetViewController *)initWithNumber:(NSInteger)number {
self = [super initWithNibName:nil bundle:nil];
if (self != nil) {
self.number = number;
}
return self;
}

Related

How to intilized controller with initWithmyData and with initWithStyle:UITableViewStyleGrouped?

I have one controller inheriting UITableViewController, and call the object as below
editAlarm *ob = [[editAlarm alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:ob];
ob.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nc animated:YES];
[ob release];
ob = nil;
[nc release];
nc = nil;
now the table will initialized with groupstyle, but I want an object with it my data as normally I do in many of my projects
-(id)initwithData:(myFaceData *)dat{
id i=[super init];
self.data=dat; where data is object having some variables in it
return i;
}
myFaceData *data=[myArray objectAtIndex:tag];
editAlarm *ob = [[editAlarm alloc] initwithData:data];
[self.navigationController pushViewController:details animated:NO];
Now How can I do two init at same time, what is solution so that my object will pass along with init of new class(Controller)
If someone is not clear about question, then feel free to ask in comments
Thanks in Advance :-)
What you could do is make your data variable a Property (and synthesise it) so you can set it right after you init it in the class you are initialising it.
Alternatively you could create your own init method as follows:
- (id)initWithStyle:(UITableViewStyle)style andData:(myFaceData *)dat {
if ((self = [super initWithStyle:style])) {
self.dat = dat;
}
return self;
}
And to call your initialiser you could do this in the calling class (where theData is the data to be passed across):
editAlarm *ob = [editAlarm alloc] initWithStyle:UITableViewStyleGrouped andData:theData];

Pass an int between a view and it's subview

I have a view called PatternsViewController and a subview named SolutionsViewController. I want to pass a variable in PatternsViewController named iteration to my SolutionsViewController, right before I present it with
solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:solutions animated:YES];
solutions = [[SolutionsViewController alloc] init];
solutions.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
// Set your value here
[solutions setMyIntWithSomeMethodIWrote:123];
[self presentModalViewController:solutions animated:YES];
And in SolutionsViewController
- (void)setMyIntWithSomeMethodIWrote:(int)value {
myInstanceVar = value;
}
I figured it out by slightly modifying Squeegy's code.
In PatternsViewController.m
[solutions setIteration:iteration];
and in SolutionsViewController.m
-(void) setIteration:(int)value {
iteration = value;
NSLog(#"Iteration set from value: %d" , iteration);
}
I would use a Singleton class.
What should my Objective-C singleton look like?
Then you can do like:
[SingletonClass sharedInstance].var = iteration;
And access it with:
[SingletonClass sharedInstance].var
Why not simply over-ride the init with an additional argument that take the int you want to set? This allows a clean instantiation without an added set call.
solutions = [[SolutionsViewController alloc] initWithIntVal: int];
The selected answer works for me but it is giving me a semantic warning. I'm a little annal retentive about warnings even if the code works so I am wondering if there is a way to make it work without the warning.
The warning is:
Instance method '-SetPrompt:' not found (return type defaults to 'id')
Here is what I did while following along to the answer in this question.
In the calling .m file
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
vcSelection *ViewSelection = [[vcSelection alloc] initWithNibName:#"vcSelection" bundle:nil];
ViewSelection.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceType])
{
[ViewSelection SetPrompt:#"Select the device type."];
}
else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceManufacturer])
{
[ViewSelection SetPrompt:#"Select the device manufacturer."];
}
else if ([[SettingsTableData objectAtIndex:indexPath.row] isEqualToString:DeviceModel])
{
[ViewSelection SetPrompt:#"Select the device model."];
}
[self.view addSubview:ViewSelection.view];
}
In the receiving .m file
#implementation vcSelection
NSMutableString *Prompt;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Debug"
message:Prompt
delegate:self
cancelButtonTitle:#"Done"
otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void) SetPrompt:(NSMutableString *)Value
{
Prompt = Value;
}
#end

UIView Does Not Rotate

I have the following code in a UIViewController
Notepad *notepad = [[Notepad alloc] initForNewTopLevelTask:0 andDAO:self.dao];
[self.navigationController pushViewController:notepad animated:YES];
[notepad release];
The initForNewTopLevelTask:andDAO: method is:
- (id) initForNewTopLevelTask:(int) theTableSize andDAO:(DAO*) aDAO {
self.dao = aDAO;
tableSize = [[NSNumber alloc] initWithInt:theTableSize];
self.isNew = [NSNumber numberWithBool:YES];
self.isSubtask =[NSNumber numberWithBool:NO];
return self;
}
When I rotate the view nothing happens, it does not rotate. If I change the UIViewController line to:
Notepad *notepad = [[Notepad alloc] init];
It rotates fine!
The view is not part of a Tab Controller and I have implemented:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
you should always call up to a parent class init method in your own init methods. Atleast for any object that is at some point extending from NSObject.
See http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-SW4
Change your init function to:
- (id) initForNewTopLevelTask:(int) theTableSize andDAO:(DAO*) aDAO {
if ( self = [super init] ) {
self.dao = aDAO;
tableSize = [[NSNumber alloc] initWithInt:theTableSize];
self.isNew = [NSNumber numberWithBool:YES];
self.isSubtask =[NSNumber numberWithBool:NO];
}
return self;
}

NSInvalidArgumentException

I Am creating an iPhone application which displays an UITableView at the start with some text in the cells. When a user taps a cell it should transition to another view. When I run the application in the iPhone Simulator and click on a cell I get the following error:
2009-09-23 12:20:03.554 ZDFMobiel[751:20b] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** -[RootViewController eigenRisicoView]:
unrecognized selector sent to instance 0xd1d1a0'
2009-09-23 12:20:03.555 ZDFMobiel[751:20b] Stack: (
Here is the function (In RootViewController.m) where the error occurred. It happens at the
if(self.eigenRisicoView == nil) {
line or the at the other if statements, depending on which cell you click.
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ZDFMobielAppDelegate *appDelegate = (ZDFMobielAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *content = (NSString *)[appDelegate.menuItems objectAtIndex:indexPath.row];
switch (indexPath.row) {
case 0:
if(self.eigenRisicoView == nil) {
EigenRisicoViewController *viewController = [[EigenRisicoViewController alloc] initWithNibName:#"EigenRisicoViewController" bundle:[NSBundle mainBundle]];
self.eigenRisicoView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.eigenRisicoView animated:YES];
self.eigenRisicoView.title = content;
break;
case 1:
if(self.declaratieStatusView == nil) {
DeclaratieStatusViewController *viewController = [[DeclaratieStatusViewController alloc] initWithNibName:#"DeclaratieStatusViewController" bundle:[NSBundle mainBundle]];
self.declaratieStatusView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.declaratieStatusView animated:YES];
self.declaratieStatusView.title = content;
break;
case 2:
if(self.vergoedingenView == nil) {
VergoedingenViewController *viewController = [[VergoedingenViewController alloc] initWithNibName:#"VergoedingenViewController" bundle:[NSBundle mainBundle]];
self.vergoedingenView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.vergoedingenView animated:YES];
self.vergoedingenView.title = content;
break;
case 3:
if(self.zoekenZorgInstView == nil) {
ZoekenZorgInstViewController *viewController = [[ZoekenZorgInstViewController alloc] initWithNibName:#"ZoekenZorgInstViewController" bundle:[NSBundle mainBundle]];
self.zoekenZorgInstView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.zoekenZorgInstView animated:YES];
self.zoekenZorgInstView.title = content;
break;
default:
break;
}
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];
}
This is my RootControllerView.h file
#import "EigenRisicoViewController.h";
#import "DeclaratieStatusViewController.h";
#import "VergoedingenViewController.h";
#import "ZoekenZorgInstViewController.h";
#import "ZDFMobielAppDelegate.h";
#interface RootViewController : UITableViewController {
EigenRisicoViewController *eigenRisicoView;
DeclaratieStatusViewController *declaratieStatusView;
VergoedingenViewController *vergoedingenView;
ZoekenZorgInstViewController *zoekenZorgInstView;
}
#property(nonatomic,retain) EigenRisicoViewController *eigenRisicoView;
#property(nonatomic,retain) DeclaratieStatusViewController *declaratieStatusView;
#property(nonatomic,retain) VergoedingenViewController *vergoedingenView;
#property(nonatomic,retain) ZoekenZorgInstViewController *zoekenZorgInstView;
#end
I Am new to iPhone development, Objective C and XCode so I don't understand what the error means..
Does anyone know what I am doing wrong?
I'd need to see the whole implementation, but it sounds like you didnt #synthesize eigenRisicoView;

UIPageControl with UIView with button

I'm new to Objective-C and need help!
I'm following the example "PageControl" which can be found on the net. I added a button to the view in the NIB and hooked up an action which the implementation is found below.
Here is my definition for the view controller being displayed in the page control:
//ContactCardViewController.h
#interface ContactCardViewController : UIViewController
{
int pageNumber;
NSMutableString *s;
}
//ContactCardViewController.m implementation
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
s = [[NSString alloc] initWithFormat:#"page: %d", page];
}
return self;
}
- (id)initWithString:(NSDictionary *)_s {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
s = t;
}
return self;
}
-(IBAction)callOrAddToContacts:(id)sender
{
jobtitleLabel.text = s;
}
//AppDelegate.m
//in delegate that loads the scroll view with the view for the current page:
- (void)loadScrollViewWithPage:(int)page {
//init the view this way the page: and the correct page number is displayed
controller = [[ContactCardViewController alloc] initWithPageNumber:page ];
//init the view this way, the value of the first person is always displayed
controller = [[ContactCardViewController alloc] initWithString:[[self.allNames objectAtIndex:page] objectForKey:#"firstname"]];
}
Please help me to understand why when the view is created with initWithString and then accessed via the button action only value for the first person in the list is always returned. When i init the view using initWithPageNumber s has the correct value Page: X.
In the InitWithString code, you're passing in a Dictionary, not a string.
- (id)initWithString:(NSDictionary *)_s {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
s = t;
}
return self;
}
You may want to change that to NSString, or change the NSMutableString... to an instance of NSDictionary. One or the other.
Found out the problem was in the plist file. The code is fine.